Nginx reverse proxy and missing trailing slash redirection

nginxreverse-proxyweb-server

I've a little problem with nginx, acting both as reverse proxy and proxified server.

On the front server, my configuration is:

server {
        server_name example.com;

        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://10.2.2.1:8999;
        }
}

And on the proxified server:

server {
    listen 10.2.2.1:8999;

    server_name example.com;
    autoindex on;

    location / {
        root /var/www/;
    }
}

Finally, on this proxified server, a directory /var/www/test exists with some files inside.

The problem occurs when I try to access to http://example.com/test (without trailing slash!): my browser is redirected to http://example.com:8999/test/… If I try to access to http://example.com/test/, no problem.

I've tried proxy_redirect statement which, from what I understand, allow the reverse proxy to rewrite Location header to avoid this kind of behavior, but this didn't worked for me. why?.

My second question is, why profixied nginx redirect me to this URL ? How I can prevent it to do that ?

Thanks by advance for any help.

Best Answer

Turn off port_in_redirect on the backend server.

Related Topic