Home  >  Q&A  >  body text

nginx - django redirect to relative path

The deployment environment is Nginx gunicorn Django

Nginx configuration is as follows

location /app1 {
        rewrite ^/app1/(.*)$ / break;
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_redirect off;
        proxy_pass http://11.11.11.1:8001;
}
location /app2 {
        rewrite ^/app2/(.*)$ / break;
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_redirect off;
        proxy_pass http://11.11.11.1:8002;
}

app1 and app2 are different applications. The following are the problems encountered

When accessing /app1/test, Django will add slash at the end and redirect. The problem is that it will redirect /test/ instead of / app1/test/
When accessing /app1/admin, it will be redirected to /admin/login/?next=/admin/.

In my own program, for example, when Oauth authentication requires a redirection URI, I think of passing $http_host/app1 through X-host## when using Nginx proxy. # Passed to the backend Django, and then get the real URI.

But the redirection inside Django doesn’t know how to pass the real URI and path, or it can use relative paths instead of absolute paths when redirecting inside Django.

黄舟黄舟2712 days ago709

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 17:26:35

    https://docs.djangoproject.com/en/dev/ref/settings/#use-x-forwarded-ho...

    https://docs.djangoproject.com/en/1.8/ref/settings/#force-script-name

    reply
    0
  • Cancelreply