2013-05-14 95 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