nginx反向代理服务器获取不到端口的问题的解决办法

使用nginx为反向代理服务器时,后端应用程序获取不到请求端口的解决办法。 

以下是nginx 简单的配置 
server { 
        listen       81; 
        server_name  localhost; 
        location / { 
          proxy_set_header Host $host; 
          proxy_set_header X-Forwarded-For $remote_addr; 
          proxy_pass http://127.0.0.1:9380; 
        } 


把第5行 的 proxy_set_header Host $host; 修改为  proxy_set_header Host $host:$server_port; 即可。 

原因是$host参数不包含端口号导致请求头部Host中的端口号丢失从而使后端程序不能正确的获取端口号。 

使用 CXF 实现webservice 时 通过nginx 反向代理 动态生成的wsdl文件中 location地址中的端口号也会丢失,从而导致 webservice 调用失败。 

$host 参数的解释 
This variable is equal to line Host in the header of request or name of the server processing the request if the Host header is not available. 

This variable may have a different value from $http_host in such cases: 1) when the Host input header is absent or has an empty value, $host equals to the value of server_name directive; 2)when the value of Host contains port number, $host doesn't include that port number. $host's value is always lowercase since 0.8.17. 

原文地址:https://www.cnblogs.com/zhengchunyuan/p/10136276.html