nginx做非80端口轉發

nginx可以很方便的配置成反向代理服務器


server {
listen 80;
server_name localhost;

location / {
    proxy_pass http://147.16.24.175:9500;
    proxy_set_header Host $host:80;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Via "nginx";

}
}


但是如果nginx的監聽端口不是默認的80端口,改爲其他端口如81端口。

後端服務器中request.getServerPort()無法獲得正確的端口,返回的仍然是80;

在response.sendRedirect()時,客戶端可能無法獲得正確的重定向url。

正確的配置方法爲

在 $host之後加上端口號,如$host:81

server {
        listen       83;
        server_name  localhost;

        location / {
         proxy_pass  http://147.16.24.175:9500;
         proxy_set_header   Host             $host:83;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
         proxy_set_header Via    "nginx";

        }
 }

摘自紅色***聯盟(www.7747.net) 原文:http://www.7747.net/Article/201104/88612.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章