nginx静态索引重定向 [英] nginx static index redirect

查看:91
本文介绍了nginx静态索引重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很荒谬,但在一个多小时的搜索中,我没有找到有效的答案.

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

我有一个运行nginx的静态网站(恰好在Varnish后面).索引文件称为index.html.我想将实际访问URL mydomain.com/index.html的任何人重定向回mydomain.com.

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.

这是我针对该站点的Nginx配置:

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使用位置为http://www.mydomain.com/301做出了预期的响应,但不幸的是http://www.mydomain.com/还会返回301,因此我们获得了重定向循环.

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.

如果index.html确实在请求中,我如何告诉nginx仅提供301服务?

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

推荐答案

添加一个新的位置块来处理您的主页,并使用try_files指令(而不是"index index.html;")来查找index.html文件直接地.请注意,try_files要求您至少输入2个选项.所以我把同一个文件放了两次.

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

[更新] 重定向循环的根本原因是'index'指令,该指令触发nginx再次进行另一轮位置匹配.这就是再次执行位置块外部的重写规则,从而导致循环的方式.因此,"index"指令就像"rewrite ... last;".指示.您不希望自己遇到这种情况.

[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.

诀窍是不要再次触发其他位置匹配. try_files可以有效地做到这一点.这就是为什么我在原始答案中选择它的原因.但是,如果您愿意,另一个简单的解决方法是替换

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;

作者

  rewrite ^/$ /index.html break;

在您原始的位置/"块内.这个重写...中断;"指令将使nginx停留在相同的位置块内,有效地停止循环.但是,这种方法的副作用是您失去了'index'指令提供的功能.

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 ]

实际上,索引指令在重写指令之后执行.因此以下内容也适用.请注意,我只是添加了rewrite ... break;线.如果请求uri为"/",则nginx首先从重写规则中找到现有文件/index.html.因此,永远不会为该请求触发index指令.结果,这两个指令可以一起工作.

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;
  }

这篇关于nginx静态索引重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆