今天,配置nginx反向代理時遇到一個問題,當設定nginx監聽80端口時轉發請求沒有問題。但若設定為監聽其他端口,就一直跳轉不正常;如,通路歡迎頁面時應該是重定向到登入頁面,在這個重定向的過程中端口丢失了。
這裡給出一個簡短的解決方案,修改nginx的配置檔案。
一、配置檔案:
<code> </code><code># the 8111 port</code>
<code> </code><code>server {</code>
<code> </code><code>listen 8111;</code>
<code> </code><code>server_name wetest.cn;</code>
<code> </code>
<code> </code><code>location </code><code>/honetest</code> <code>{</code>
<code> </code><code>proxy_pass http:</code><code>//11</code><code>.42.30.30:8080</code><code>/honetest</code><code>;</code>
<code> </code><code>proxy_set_header X-Real-IP $remote_addr;</code>
<code> </code><code>proxy_set_header Host $host:$server_port; </code><code>#這裡是重點,這樣配置才不會丢失端口 </code>
<code> </code><code>}</code>
<code> </code><code>location = </code><code>/50x</code><code>.html {</code>
<code> </code><code>root html;</code>
<code> </code><code>}</code>
二、産生原因:
nginx沒有正确的把端口資訊傳送到後端,沒能正确的配置nginx,下面這行是關鍵
proxy_set_header Host $host:$server_port; 這一行是關鍵。
本文轉自ling118 51CTO部落格,原文連結:http://blog.51cto.com/meiling/1978395,如需轉載請自行聯系原作者