nginx static index redirect nginx static index redirect nginx nginx

nginx static index redirect


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.htmlHTTP/1.1 301 Moved PermanentlyServer: nginxDate: Sat, 16 Mar 2013 09:07:27 GMTContent-Type: text/htmlContent-Length: 178Connection: keep-aliveLocation: http://www.mydomain.com/HTTP/1.1 200 OKServer: nginxDate: Sat, 16 Mar 2013 09:07:27 GMTContent-Type: text/htmlContent-Length: 4Last-Modified: Sat, 16 Mar 2013 08:05:47 GMTConnection: keep-aliveAccept-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;  }


Looks like you really don't want index.php to show up in the address bar, is that correct?

If you add a rewrite directive to the nginx config, you'll get a redirect loop, as you have experienced. If you are open to a javascript solution, you can place this anywhere in your index.html to silently rewrite the address bar:

<script>    history.pushState(null, '', '/');</script>

For more information

Keep in mind that while most modern browsers support the history API, not all do (namely, most versions of IE).