nginx/index.html到/重写

nginx/index.html到/重写,nginx,rewrite,Nginx,Rewrite,我正试图将/index.html重写为/用于SEO目的(愚蠢的搜索引擎将index.html与/混淆,并惩罚重复的内容)——也为了协调web分析数据 我尝试了在stackoverflow、nginx文档等上找到的所有解决方案,但都没有成功。我想我一定有其他配置问题或者其他一些非常明显的问题。这是我第一次安装nginx——用于Apache和IIS 以下是我的default.conf: server { listen 80; server_name web.local;

我正试图将/index.html重写为/用于SEO目的(愚蠢的搜索引擎将index.html与/混淆,并惩罚重复的内容)——也为了协调web分析数据

我尝试了在stackoverflow、nginx文档等上找到的所有解决方案,但都没有成功。我想我一定有其他配置问题或者其他一些非常明显的问题。这是我第一次安装nginx——用于Apache和IIS

以下是我的default.conf:

server {
    listen       80;
    server_name  web.local;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
这是我的virtual.conf(注释掉的部分是我最近的一次尝试——未注释时,在您尝试访问www.domain.com/index.html时会出现301 Moved permanually错误):

cobaco解决方案的HTTP响应头:

URL:
http://www.domain.com
http/1.1 301 moved permanently
server: nginx/1.2.8
date: Thu, 16 May 2013 01:42:58 GMT
content-type: text/html
content-length: 184
connection: keep-alive
location: http://domain.com/

Redirecting URL:
http://domain.com/
http/1.1 301 moved permanently
server: nginx/1.2.8
date: Thu, 16 May 2013 01:42:58 GMT
content-type: text/html
content-length: 184
connection: keep-alive
location: http://www.domain.com/
我认为这一行可能会引起问题:“location=/index.html{return301$scheme://domain.com/所以我在“scheme://”之后加上了www!这导致以下HTTP响应头:

URL:
http://www.domain.com
http/1.1 301 moved permanently
server: nginx/1.2.8
date: Thu, 16 May 2013 01:42:58 GMT
content-type: text/html
content-length: 184
connection: keep-alive
location: http://www.domain.com/

Redirecting URL:
http://www.domain.com/
http/1.1 301 moved permanently
server: nginx/1.2.8
date: Thu, 16 May 2013 01:42:58 GMT
content-type: text/html
content-length: 184
connection: keep-alive
location: http://www.domain.com/
经过进一步的修改,下面的配置完成了我希望它完成的任务,但由于if语句的原因,它并不理想。有什么建议吗

server {
  server_name  www.domain.com;
  root /var/www/html/domain.com;
  index index.html;
  if ($request_uri = /index.html) {
      return 301 http://www.domain.com/;
  }
  #location = /index.html {
  #    return 301 $scheme://www.domain.com/;
  #}
}

server {
  listen 80;
  server_name domain.com;
  return 301 $scheme://www.domain.com$request_uri;
}

301并不是一个真正的错误,它只是一个标题,告诉浏览器它需要重定向到新的目的地,web浏览器会自动且无声地处理这些标题,但是如果你正在编写一些curl应用程序,那么你应该指示它尊重并处理这些标题。 它是301,因为您在配置中写入
永久
,302是
临时

当我尝试重写时,它对我起作用,但我在非重定向服务器中使用return而不是rewrite

location = /index.html {
    return 301 $scheme://$host;
}
另外,如果您将重定向服务器更改为使用return也会更好

server {
    server_name domain.com;
    return 301 $scheme://www.domain.com$request_uri;
}

编辑:如@cobaco所建议,将if块更改为location块,我不知道为什么我错过了这样一个愚蠢的错误

server {
  server_name  www.domain.com;
  root /var/www/html/domain.com;
  index index.html;
  location = /index.html {return 301 $scheme://www.domain.com/;}
}

server {
  listen 80;
  server_name domain.com;
  return 301 $scheme://www.domain.com$request_uri;
}
注意:

  • 尽可能使用
    位置
    块而不是
    如果
    (因为
    位置
    内的
    如果
    已知会导致问题,请参阅以了解详细信息)
  • 对301使用
    return
    not
    rewrite
    (因为这样效率更高)
  • 使用内置变量而不是正则表达式匹配(因为更有效,请参阅以获取内置变量列表)
  • 根指令
    索引
    指令通常应始终位于
    服务器
    块的主级(否则需要为每个子块重复它们)

您的最终解决方案完全正确

if
指令只有在
位置
块内时才是邪恶的。另外,在
if
块中只有
return
指令。我看不出有什么问题。参考:

cobaco解决方案中的无限重定向循环是因为

  index  index.html;

触发另一轮位置匹配。因此,nginx在重定向到
location=/index.html之后,将再次被困在
location=/index.html
中http://www.domain.com/

谢谢!我实现了您的解决方案,并认为我遵循了逻辑,但出于某种原因,我使用此方法和我尝试过的任何其他(合法但可能效率较低)重定向方法获得了无限重定向。我在我的OP中发布了你建议的HTTP响应,因为在评论中格式似乎不可能。
  index  index.html;