99

I'm running nginx in a Virtual Machine using NAT and I'm having redirection issues when I access it from the host machine.

Works as expected

  • http://localhost:8080/test/index.htm: works.
  • http://localhost:8080/test/: works.

Doesn't work as expected

  • http://localhost:8080/test: redirects to http://localhost/test/ . This is not what I want. (notice it strips the port number)

What I've tried

Based on what I've googled, I tried server_name_in_redirect off; and rewrite ^([^.]*[^/])$ $1/ permanent;, both with no success.

My default.conf:

server {
    listen       80;
    server_name  localhost;
    # server_name_in_redirect off;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
    # rewrite ^([^.]*[^/])$ $1/ permanent;
        root           /usr/share/nginx/html;
        try_files      $uri =404;
        #fastcgi_pass   127.0.0.1:9000;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
        include        fastcgi_params;
    }


    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

4 Answers 4

43

A somewhat simpler solution, that worked for me, is to disable absolute redirects with absolute_redirect off; as in the following example:

server {
    listen 80;
    server_name  localhost;
    absolute_redirect off;

    location /foo/ {
        proxy_pass http://bar/;
    }

If I run curl on on http://localhost:8080/foo, I can see that the Location header in the redirect HTTP response is given as /foo/ and not http://localhost/foo/.

$ curl -I http://localhost:8080/foo
HTTP/1.1 301 Moved Permanently
Server: nginx/1.13.8
Date: Tue, 03 Apr 2018 20:13:28 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: /foo/

From that, I assume any web-browser would do the right thing with the relative location. Tested on Chrome and it works fine.

7
  • 18
    This still returns a 301
    – Dave
    Apr 26, 2018 at 22:12
  • 1
    Here's an explanation of the issue, and an actual solution: serverfault.com/a/812461 May 29, 2018 at 17:16
  • 6
    From my understanding the question is not actually complaining about the 301 itself. The 301 is fine, and expected, the problem is that the redirection should point to the same server name keeping the port 8080.
    – guiccbr
    Jul 12, 2018 at 22:20
  • 2
    This works fine for me. For the extra paranoid, you can keep absolute_redirect off scoped to a location {} block if you just need it for that particular location Oct 8, 2018 at 17:46
  • 2
    This is a working solution for most cases. For me, the server is behind a proxy that terminates SSL. without absolute_redirect off; NGINX always redirect to http:// because it does not know the browser is using https://. Jul 28 at 2:04
43

I posted a possible solution to this problem on serverfault; reproduced here for convenience:

If I understand the question correctly, you want to automatically serve, without using a 301 redirect, http://example.com/foo/index.html when the request is for http://example.com/foo with no trailing slash?

Basic solution that works for me

If so I've found this try_files configuration to work:

try_files $uri $uri/index.html $uri/ =404;
  • The first $uri matches the uri exactly
  • The second $uri/index.html matches a directory containing the index.html where the last element of the path matches the directory name, with no trailing slash
  • The third $uri/ matches the directory
  • The fourth =404 returns the 404 error page if none of the preceding patterns match.

Taken from Serverfault answer

My updated version

If you add in the server block:

index index.html index.htm;

And modify try_files to look like this:

try_files $uri $uri/ =404;

It should work too.

8
  • @maiconsanson - it seems to work for me with subfolders. Feel free to explain further. Sep 3, 2015 at 20:37
  • 2
    Sorry, I mean, it works but the images inside the index.html are not found. By the way, I'm using to solve this with Dokku: if (-d $request_filename) { rewrite [^/]$ $scheme://$http_host$uri/ permanent; } buildpack-nginx Sep 4, 2015 at 0:24
  • It's likely that the relative paths in the index.html are broken somehow. Inspect the url that the page is trying to retrieve, and compare with the url that the image is actually at, and see if the issue becomes clearer. Sep 4, 2015 at 0:32
  • The urls is alright (1.png and so on). Everything is in the root app (index.html and images). Sep 4, 2015 at 0:45
  • 1
    Only try_files $uri $uri/index.html $uri/ =404; worked for me. The second solution did not.
    – Pete
    Apr 26, 2021 at 17:42
12

try :

server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
        if (-d $request_filename) {
            rewrite [^/]$ $scheme://$http_host$uri/ permanent;
        }
    }
}
5

Try changing

server_name  localhost;
# server_name_in_redirect off;

to

server_name  localhost:8080;
server_name_in_redirect on;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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