How to handle redirect response correctly when using Nginx as a proxy, django as a backend

I have a Django app, and recently I need to run a beta. I want the current running application not to touch, and the redirection of all requests starts with "/ beta" in the beta application using Nginx. Here is my conf

location / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_connect_timeout 10; proxy_read_timeout 360; proxy_pass http://localhost:8000/; } location /beta/ { rewrite ^/beta/(.*)$ /$1 break; proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_connect_timeout 10; proxy_read_timeout 360; proxy_pass http://localhost:8001/; } 

This works, but there is a problem when the application returns a 301 response, basically when the user needs to log in to access some resource, the URL becomes old.

For example, if /events login is required.

http://example.com/beta/eventshttp://example.com/login?next=/events/

How can I fix this without changing the application code? (Nginx solution?)

+4
source share
1 answer

try proxy_redirect .

"This directive sets the text that should be changed in the response header" Location "and" Refresh "in the response of the proxy server."

So

  proxy_redirect http://example.com/ http://example.com/beta/; 

Of course, this only applies to redirects issued by the proxy server. I also assume that all redirects have the same problem.

tip: if necessary, you can use more than one proxy_redirect directive.

+6
source

Source: https://habr.com/ru/post/1481960/


All Articles