2012-07-02 62 views
0

我試圖用nginx將我的主頁(www.domain.com)重定向到一個子目錄(www.domain.com/store)。我有重定向工作,但每當我使用域名時,它都會將我重定向到IP地址(www.IP.com/store)。這是我的服務器nginx配置。預先感謝您的任何幫助!Nginx server_name不適用於重寫子目錄。

server { 
    listen  80 default_server; 
    server_name *.domain.com; 

    location/{ 
     index index.php index.html index.htm; 
    } 
    location =/{ 
     rewrite ^/store permanent; 
    } 

    root /usr/local/www/nginx; 

}

回答

0

你錯過了一個空間。 rewrite ^/store permanent;將嘗試在uri路徑的開頭匹配'/ store'(感謝^),如果匹配,它會將其重寫爲'permanent'。由於這是在location = /之內,它永遠不會成功。相反,你需要:

rewrite^/store permanent; 
+0

嘿!感謝您的答覆。我改變了你所說的配置,但似乎沒有解決問題。 – Lethjakman

+0

有兩件事:首先,重寫仍然需要位於location = /中,其次,您可能正在使用瀏覽器進行測試,而且由於您使用的是永久性重寫,因此瀏覽器甚至不會再與服務器進行檢查當你請求domain.com/時,它只會使用緩存重寫。嘗試捲曲,或者如果這不是一個選項,嘗試其他瀏覽器。 – kolbyjack

+0

我確實保留了= /中的重寫,並且我正在瀏覽器上進行測試。我試過「curl -L domain.com」,它給了我一個完整的網頁返回,但它沒有提到標題欄會說什麼。我也清除了瀏覽器的緩存。 – Lethjakman

0

嗯,我不能適應這個在你的文章,所以我想我應該把它放在這裏。這是一個很好的練習stackoverflow?我嘗試了「curl -l」,它只是返回了未被重定向時得到的「未找到」。但是我使用了「curl -liL domain.com」,這是出來的。

HTTP/1.1 301 Moved Permanently 
Server: nginx/1.2.1 
Date: Thu, 05 Jul 2012 17:30:01 GMT 
Content-Type: text/html 
Content-Length: 184 
Location: http://domain.com/store 
Connection: keep-alive 

HTTP/1.1 301 Moved Permanently 
Server: nginx/1.2.1 
Date: Thu, 05 Jul 2012 17:30:01 GMT 
Content-Type: text/html 
Content-Length: 184 
Location: http://domain.com/store/ 
Connection: keep-alive 

HTTP/1.1 301 Moved Permanently 
Server: nginx/1.2.1 
Date: Thu, 05 Jul 2012 17:30:01 GMT 
Content-Type: text/html 
Transfer-Encoding: chunked 
Connection: keep-alive 
X-Powered-By: PHP/5.4.4 
Expires: Mon, 26 Jul 1997 05:00:00 GMT 
Last-Modified: Thu, 05 Jul 2012 17:30:01 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA" 
Set-Cookie: xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae; path=/store; domain=IP; httponly 
Location: http://IP/store/?xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae 

HTTP/1.1 302 Found 
Server: nginx/1.2.1 
Date: Thu, 05 Jul 2012 17:30:01 GMT 
Content-Type: text/html 
Transfer-Encoding: chunked 
Connection: keep-alive 
X-Powered-By: PHP/5.4.4 
Expires: Mon, 26 Jul 1997 05:00:00 GMT 
Last-Modified: Thu, 05 Jul 2012 17:30:01 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA" 
Set-Cookie: xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae; path=/store; domain=IP; httponly 
Location: http://IP/store/?xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae 

HTTP/1.1 301 Moved Permanently 
Server: nginx/1.2.1 
Date: Thu, 05 Jul 2012 17:30:01 GMT 
Content-Type: text/html 
Transfer-Encoding: chunked 
Connection: keep-alive 
X-Powered-By: PHP/5.4.4 
Expires: Mon, 26 Jul 1997 05:00:00 GMT 
Last-Modified: Thu, 05 Jul 2012 17:30:01 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA" 
Set-Cookie: xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae; path=/store; domain=IP; httponly 
Location: /store/home.php 

HTTP/1.1 200 OK 
Server: nginx/1.2.1 
Date: Thu, 05 Jul 2012 17:30:02 GMT 
Content-Type: text/html; charset=UTF-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
X-Powered-By: PHP/5.4.4 
Expires: Mon, 26 Jul 1997 05:00:00 GMT 
Last-Modified: Thu, 05 Jul 2012 17:30:01 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA" 
Set-Cookie: xid_1f463=e7d9abf146153d7a49e3f08bda47c008; path=/store; domain=IP; httponly 
Set-Cookie: RefererCookie=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/store; domain=IP; httponly 
Set-Cookie: store_language=en; expires=Fri, 05-Jul-2013 17:30:01 GMT; path=/store; domain=IP