Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <pre><code>location = / { root /usr/share/nginx/www.mydomain.com/public; try_files /index.html /index.html; } </code></pre> <p>Looks good based on my experiment:</p> <pre><code>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 </code></pre> <p>[<strong>UPDATE</strong>] 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.</p> <p>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</p> <pre><code> index index.html; </code></pre> <p>by</p> <pre><code> rewrite ^/$ /index.html break; </code></pre> <p>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.</p> <p>[<strong>UPDATE 2</strong>]</p> <p>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.</p> <pre><code> location / { root /usr/share/nginx/www.mydomain.com/public; index index.html; rewrite ^/$ /index.html break; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload