2011-06-07 113 views
1

我是nginx新手,我已經安裝了php(使用端口8888)和nginx(使用端口80),並且我有一個靜態html頁面,如www.aa.com:8888/news /html/about/aboutus/index.html,現在我想使用www.aa.com/test/aboutus.html來訪問它,並且在執行此操作時,瀏覽器中的URL不應更改。這個怎麼做?nginx重寫url問題需要幫助

我嘗試了一些辦法,但在瀏覽器中的鏈接將只是重定向到新的URL字符串...在如下Nginx的conf文件

我的示例配置:

location ^~ /test/aboutus.html { 
     proxy_pass http://127.0.0.1:8888; 
     proxy_redirect default; 
     rewrite ^/test/aboutus.html$ /news/html/about/aboutus last; 
     break; 
    } 

謝謝很多!

回答

1

沒有人回答,所以我想我會給它一個鏡頭。

根據the documentation for proxy_pass

一種特殊的情況是在proxy_pass語句中使用的變量:不使用請求的URL,你是全權負責自己構建的目標URL。

所以,如果你能在那裏得到一個變量,你可以提供完整的URL,而不是擔心rewrite。事情是這樣的:

location /test/aboutus.html { 
    proxy_pass $scheme://127.0.0.1:8888/news/html/about/aboutus; 
} 

我沒有測試過這一點(對不起)。

[更新]

還有一個想法:

location /test/aboutus.html { 
    proxy_pass $scheme://127.0.0.1:8888/news/html/about/aboutus; 
    proxy_redirect $scheme://127.0.0.1:8888/news/html/about/aboutus $scheme://$host/test/aboutus.html 
} 

的想法是Location頭的答覆映射回你希望客戶端在瀏覽器中顯示的內容。這就是proxy_redirect的用途(雖然「默認」設置應該工作,我認爲)。

+0

感謝您的回覆!我測試了一下,在我的本地環境中,瀏覽器中的URL將從http://localhost/test/aboutus.html更改爲http://127.0.0.1:8888/news/html/about/aboutus,不確定是否這是我的nginx環境問題... 我只是想要的URL不會改變... – xeoshow 2011-06-09 02:49:25

+0

真的嗎?即使沒有'rewrite'規則?這很奇怪...... – Nemo 2011-06-09 03:20:05

+0

是的,完全複製你上面寫的東西。我正在測試Windows XP。 – xeoshow 2011-06-09 04:08:49