Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx static index redirect

This seems ridiculous but I've not found a working answer in over an hour of searching.

I have a static website running off nginx (which happens to be behind Varnish). The index file is called index.html. I want to redirect anyone who actually visits the URL mydomain.com/index.html back to mydomain.com.

Here is my nginx config for the site:

server {
  listen  8080;
  server_name  www.mydomain.com;
  port_in_redirect  off;

  location / {
    root   /usr/share/nginx/www.mydomain.com/public;
    index index.html;
  }

  rewrite /index.html http://www.mydomain.com/ permanent;
}

http://www.mydomain.com/index.html responds as expected with a 301 with the location http://www.mydomain.com/ but unfortunately http://www.mydomain.com/ also serves a 301 back to itself so we get a redirect loop.

How can I tell nginx to only serve the 301 if index.html is literally in the request?

like image 725
Ade Avatar asked Mar 19 '13 01:03

Ade


People also ask

How do I serve index html with NGINX?

html file with Nginx. To follow the procedure of serving HTML files, we will create a “www” directory using the “mkdir” command. The “mkdir” command is utilized in Linux-based systems such as CentOS for creating one or more directories. Now, press “CTRL+O” to save the content we have added in the “index.

How do I redirect http to https in NGINX?

Redirect HTTP to HTTPS version for Specified domain in Nginx Server_name domain-name.com www.domain-name.com – it specifies the domain names. So, replace it with your website domain name that you want to redirect. Return 301 https://domain-name.com$request_uri – it moves the traffic to the HTTPS version of the site.


1 Answers

Add a new location block to handle your homepage, and use try_files directive (instead of "index index.html;") to look for the index.html file directly. Note that try_files requires you to enter at least 2 choices. So I put the same file twice.

location = / {
  root   /usr/share/nginx/www.mydomain.com/public;
  try_files /index.html /index.html;
}

Looks good based on my experiment:

curl -iL http://www.mydomain.com/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sat, 16 Mar 2013 09:07:27 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.mydomain.com/

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 16 Mar 2013 09:07:27 GMT
Content-Type: text/html
Content-Length: 4
Last-Modified: Sat, 16 Mar 2013 08:05:47 GMT
Connection: keep-alive
Accept-Ranges: bytes

[UPDATE] The root cause of the redirect loop is the 'index' directive, which triggers nginx to do another round of location match again. That's how the rewrite rule outside the location block gets executed again, causing the loop. So the 'index' directive is like a "rewrite...last;" directive. You don't want that in your case.

The trick is to not trigger another location match again. try_files can do that efficiently. That's why I picked it in my original answer. However, if you like, another simple fix is to replace

  index index.html;

by

  rewrite ^/$ /index.html break;

inside your original "location /" block. This 'rewrite...break;' directive will keep nginx stay inside the same location block, effectively stop the loop. However, the side effect of this approach is that you lose the functionality provided by 'index' directive.

[UPDATE 2]

Actually, index directive executes after rewrite directive. So the following also works. Note that I just added the rewrite...break; line. If the request uri is "/", nginx finds the existing file /index.html from the rewrite rule first. So the index directive is never being triggered for this request. As a result, both directives can work together.

  location / {
    root   /usr/share/nginx/www.mydomain.com/public;
    index index.html;
    rewrite ^/$ /index.html break;
  }
like image 195
Chuan Ma Avatar answered Sep 23 '22 19:09

Chuan Ma