Nginx做反向代理时访问端口被自动去除

时间:2022-04-16 06:56:34

使用的Nginx版本 : nginx/1.13.10

出现问题的配置文件如下

upstream http-web {
server 0.0.0.0:9000;
} server {
listen 8088;
server_name mysql_web; resolver 8.8.8.8; location / {
proxy_pass http://http-web; #Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

测试结果:

[root@nginx-server conf.d]# curl http://172.30.45.10:8088 -v

< HTTP/1.1 302 Found

< Server: nginx/1.13.10

< Date: Wed, 29 May 2019 14:32:10 GMT

< Content-Length: 0

< Connection: keep-alive

< x-frame-options: SAMEORIGIN

< Location: http://172.30.45.10/login #<== 端口被nginx去除

正确的配置文件如下:

upstream http-web {
server 0.0.0.0:9000;
} server {
listen 8088;
server_name mysql_web; resolver 8.8.8.8; location / {
proxy_pass http://http-web; #Proxy Settings
proxy_redirect off;
proxy_set_header Host $host:$server_port; #Host头部需要增加端口,否则端口会被Nginx去除
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

测试结果:

[root@nginx-server conf.d]# curl http://172.30.45.10:8088 -v

< HTTP/1.1 302 Found

< Server: nginx/1.13.10

< Date: Wed, 29 May 2019 14:32:10 GMT

< Content-Length: 0

< Connection: keep-alive

< x-frame-options: SAMEORIGIN

< Location: http://172.30.45.10:8088/login #<== 此时已恢复正常