title: How to handle redirect response properly when using Nginx as proxy server, django as backend
body: <p>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</p>
  
  <pre><code>    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/;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>For example, if <code>/events</code> is login required.</p>
  
  <p><a href="http://example.com/beta/events" rel="noreferrer">http://example.com/beta/events</a>   ->  <a href="http://example.com/login?next=/events/" rel="noreferrer">http://example.com/login?next=/events/</a></p>
  
  <p>How can I fix this without changing the application code? (Nginx solution?)</p>