2013-05-14 96 views
5

我想重写/index.html /为SEO目的(愚蠢的搜索引擎混淆了index.html /和处罚重复的内容) - 也协调网站分析数据。nginx /index.html改写/重写

我试过了我在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(注释部分是我最近一次尝试 - 未注释时,它提供一个301永久移动,当您试图访问WWW错误.domain.com/index.html中):

server { 
    listen  80; 
    server_name www.domain.com; 

    location/{ 
     root /var/www/html/domain.com; 
     index index.html; 
     #if ($request_uri = /index.html) { 
     # rewrite^http://www.domain.com permanent; 
     #} 
    } 
} 

server { 
    listen 80; 
    server_name domain.com; 
    rewrite ^/(.*) http://www.domain.com/$1 permanent; 
    } 

HTTP响应头为cobaco的溶液:

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 {return 301 $ scheme://domain.com/;}”所以我添加了www。在“scheme://”之后 - 让我知道这是不是一件坏事!这导致了以下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; 
} 

回答

4

你最终的解决方案是完全没问题的。

if指令是邪恶的,只要它在location区块内。在if区块中您也只有return指令。我没有看到任何问题。参考:http://wiki.nginx.org/IfIsEvil

在cobaco的溶液中的无限重定向循环是因为

index index.html; 

引发另一轮的位置匹配。所以nginx在被重定向到http://www.domain.com/之后会再次陷入location = /index.html

+0

谢谢,很高兴它的作品没关系! :) – auralsun 2013-05-17 00:12:57

0

301是不是一个真正的错误,这只是一个头,告诉它需要重定向到新的目的地的浏览器,浏览器会自动无声地处理这些标题,但如果你正在写一些卷曲应用程序,然后你应该指示它尊重和处理这些标题。 和它的301,因为您在配置写permanent,302 temporary

当我想你的重写它与我的工作,但我用的回报,而不是重写的非重定向服务器

location = /index.html { 
    return 301 $scheme://$host; 
} 

也是它如果你改变你的重定向服务器使用返回以及

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

编辑会更好:改变了,如果块的位置块像@cobaco建议,我不知道为什么,我错过了小号UCH一个愚蠢的错误

2

下面将做你想要什么:

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

要注意:

  • 使用location块,而不是if如果可能的话(因为if一个location内是众所周知的造成问题见http://wiki.nginx.org/IfIsEvil的细节)
  • 使用return而不是rewrite为301的(因为它是更有效)
  • 使用内建的变量,而不是正则表达式匹配(因为它的效率更高,看http://wiki.nginx.org/HttpCoreModule#Variables为内置变量列表)
  • rootindex指令通常应该始终位于server的主要水平 - 块(否则你需要重复他们为每个子块)
+1

谢谢!我实现了你的解决方案,并认为我遵循逻辑,但出于某种原因,我得到了一个无限的重定向使用这个和基本上任何其他(合法但可能效率较低)重定向方法,我试过了。我在OP中发布了你的建议的HTTP响应,因为格式在评论中似乎是不可能的。 – auralsun 2013-05-16 06:49:02