20

I currently have nginx setup to serve content through Varnish. Nginx listens on port 8000 and varnish connects users' requests from 80 to 8000.

The problem is, on some occasions, particularly when trying to hit a directory, like site.com/2010, nginx is redirecting the request to site.com:8000/2010/.

How can I prevent this?

arbales
  • 497
  • 1
  • 4
  • 16
  • I had the same problem. This worked for me: https://serverfault.com/questions/174297/nginx-apache-trailing-slash-redirect – Immuc Apr 14 '21 at 03:49

5 Answers5

26

I my case nginx listens to port 80 inside a docker container but it's mapped to port 8080 (or any random port) outside the container. There is no reverse proxy in-between that can add proper headers for port and also don't want to hardcode it in the nginx configuration.

Example of wrong redirect:

http://localhost:8080/directory -> http://localhost/directory/

I tried:

server {
  # ...
  port_in_redirect off;
  server_name_in_redirect off;
  # ...
}

But didn't work. The only thing that worked well was:

server {
  # ...
  absolute_redirect off;
  # ...
}

Manual entry forabsolute_redirect says:

If disabled, redirects issued by nginx will be relative.

I find this to be more flexible and doesn't require you to have the server name and port hardcoded anywhere.

If you are worried about redirects with relative URLs check this comment.

Luxian
  • 401
  • 4
  • 5
  • 1
    same issue, wrangling with nginx in docker.. `absolute_redirect off;` helps, however, it's if one needs occasional rewrite rule redirecting to external resource, with `absolute_redirect set` to `off`, one won't be able to. By adding it to specific `location` block, one can target specific location paths. – anapsix Apr 26 '18 at 15:43
16

I found the answer to this question by more carefully reading the HttpCoreModule docs.

port_in_redirect off;

This retains the port used by the client during redirects. Closely related is server_name_in_redirect which uses the first hostname for redirects. As I didn't want sitename.v.myserver.com to redirect to sitename.com,

MrWhite
  • 11,643
  • 4
  • 25
  • 40
arbales
  • 497
  • 1
  • 4
  • 16
3

The code behind NGINX probably uses the FASTCGI variable SERVER_PORT to determine where to direct the user. SERVER_PORT will contain the port nginx listens on, so that would be 8000 in your case.

You can try something like this for testing purpose:

location ~ \.php$ {
        [...]
        fastcgi_param  SERVER_PORT 80;
}

adapted to your configuration. That's a dirty hack but can help you diagnose the problem.

Julien Vehent
  • 2,927
  • 18
  • 26
2

For those who have

  • Varnish listens port 80
  • backend server (in my case nginx) listens port 8080

The fastcgi_param SERVER_PORT should be same as the frontend.

So, I set

fastcgi_param  SERVER_PORT 80;

to redirect to appropriate URL from nginx.

AnkitK
  • 121
  • 1
0

I have the same problem with Nginx+PHP-FPM in docker on 80:8080 (port mapping). port_in_redirect could not help. with absolute_redirect the problem was periodically.

So my solution: create file f.e. schema.conf:

map $http_x_forwarded_proto $balancer_port {
    default 80;
    "https" 443;
}
 map $http_x_forwarded_proto $balancer_https {
     default "NO";
     "https" "YES";
}

And then just use need port:

location ~ \.php$ {
        try_files       $uri @bitrix;
        fastcgi_pass    php-fpm:9000;
        fastcgi_index   index.php;
        include         fastcgi_params;
        fastcgi_param   SERVER_PORT     $balancer_port;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
itlux
  • 1