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. 

posted @   Leslie'sblogs  阅读(3735)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示