Nginx static index redirection

This seems ridiculous, but I did not find a working answer after more than an hour of searching.

I have a static website running nginx (which turns out to be behind the varnish). The index file is called index.html. I want to redirect everyone who actually visits the URL mydomain.com/index.htmlback to mydomain.com.

Here is my nginx configuration 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.htmlresponds as expected with 301the location http://www.mydomain.com/, but, unfortunately, http://www.mydomain.com/also returns 301 to itself, so we get a redirect cycle.

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

+5
source share
2 answers

try_files ( index index.html;) index.html . , try_files 2 . .

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

:

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

[ ] 'index', nginx . , , . , "index" "... "; . .

, . try_files . . , ,

  index index.html;

  rewrite ^/$ /index.html break;

"location/". "... "; nginx , . , , "index".

[ 2]

rewrite. . , ... break; . uri "/", nginx /index.html . index . .

  location / {
    root   /usr/share/nginx/www.mydomain.com/public;
    index index.html;
    rewrite ^/$ /index.html break;
  }
+7

, , index.php , ?

rewrite nginx, , . javascript, - index.html, :

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

, , API history, ( , IE).

-1

All Articles