Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle redirect response properly when using Nginx as proxy server, django as backend

Tags:

nginx

django

I have a Django application and recently I need to launch a beta version. I want to keep the current running application untouched, and redirect all request starts with "/beta" to the beta app, with the help of 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 app returns a 301 response, mostly when user needs to login to access some resource, the URL becomes the old one.

For example, if /events is login required.

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

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

like image 998
xiaowl Avatar asked May 21 '13 13:05

xiaowl


People also ask

Can I use nginx as a forward proxy?

By using the nginx forward proxy we can masking the location and IP for gaining access to services. Nginx forward proxy will continuing the request on behalf of the client. At the time when the host server will accept the request then only we can see the IP of the nginx proxy server.

What is nginx proxy redirect?

Nginx provides proxy_redirect directive which can be used in http, server, or location context. The syntax is: proxy_redirect redirect replacement. In this example, the proxied server (upstream Apache or Lighttpd) returned line Location: http://server1.cyberciti.biz:8080/app/.

What is difference between redirect and proxy?

A proxy server makes requests to another PINT server on behalf of its clients; a redirect server returns to its clients addresses of other PINT servers to which requests can be redirected.


1 Answers

try proxy_redirect.

"This directive sets the text, which must be changed in response-header "Location" and "Refresh" in the response of the proxied server."

so

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

of course, this only applies to redirects issued by the proxied server. I'm also assuming all redirects have the same problem.

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

like image 198
José Montiel Avatar answered Nov 09 '22 10:11

José Montiel