0

I am setting up a reverse proxy that receives requests from a federated authentication server and forwards them to my backend app. When the requests are missing a trailing slash, nginx does its default 301 redirect but the address it redirects to does not include the path matched for in the location block. The idea is to proxy requests from upstream.com/myApp to the root url backend.com/ in Gunicorn. My config is:

geo $allow {
    default 0;
    <upstream ip> 1;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    set_real_ip_from <backend ip>;
    real_ip_header X-Forwarded-For;

    if ($allow = 0) {
        return 444;
    }

    server_name backend.com;
    underscores_in_headers on;

    include snippets/<ssl-conf>;
    include snippets/<ssl-params>;

    location /<myApp>/static/ {
        root /<path>/<myApp>/static;
    }

    location /<myApp>/ {

        include proxy_params;
        proxy_pass_request_headers on;
        proxy_pass http://unix:/<path>/<myApp>/<myApp>.sock:/;
    }

    location = /<myApp> {

        include proxy_params;
        proxy_pass_request_headers on;
        proxy_pass 
        http://unix:/<path>/<myApp>/<myApp>.sock:/;
    }

}

Originally I had only included the first location block, but when a request from upstream.com/myApp (without a trailing slash) happened, nginx would redirect to backend.com/myApp/, and it would not forward the headers of the original request. Adding the second location block to prevent the redirect fixed that.

Now, however, if I receive a request like upstream.com/myApp/search (again, without trailing slash), it does a 301 redirect to upstream.com/search/ (adds the trailing slash, but the part is gone. How can I preserve the part in the url after nginx does the redirect?

0

I did some more debugging, and this ended up not being an Nginx problem. Rather, it's a problem of Django not appending the path filtered out by nginx proxy to redirects.

0

Its not an Nginx problem, it's an Nginx feature. If you don't want Nginx to remove the part of the path which matches your location block then your proxy_pass directive must not have a path specified after the server:port or socket.

This includes just a trailing slash on the end, anything path you specify will replace the matching part of your location block, and if you just specify a slash then it has the effect of deleting the matching part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.