nginx 反向代理時丟失端口的解決方案

    今天,配置nginx反向代理時遇到一個問題,當設置nginx監聽80端口時轉發請求沒有問題。但一旦設置爲監聽其餘端口,就一直跳轉不正常;如,訪問歡迎頁面時應該是重定向到登陸頁面,在這個重定向的過程當中端口丟失了。
    這裏給出一個簡短的解決方案,修改nginx的配置文件。

1、配置文件

# the 90 port
    server {
        listen       90; 
        server_name  zxy1994.cn;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host:$server_port; #這裏是重點,這樣配置纔不會丟失端口
        location / {
                proxy_pass http://127.0.0.1:9001;
        }
        location = /50x.html {
            root   html;
        }

    }

2、產生緣由

          nginx沒有正確的把端口信息傳送到後端,沒能正確的配置nginx,下面這行是關鍵
                 proxy_set_header Host $host:$server_port; 這一行是關鍵。
相關文章
相關標籤/搜索