nginx在使用非80端口做反向代理【转】
设置nginx反向代理,nginx在使用非80端口做反向代理时,浏览器访问发现返回302错误
upstream jboss{ server 10.79.36.119:8080 max_fails=3 fail_timeout=20s; server 10.79.36.120:8080 max_fails=3 fail_timeout=20s; check interval=3000 rise=2 fall=5 timeout=1000; } server { listen 8088; server_name 10.72.36.112; location / { proxy_pass http://jboss; proxy_redirect off; proxy_set_header Host $host; proxy_hide_header Server; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; }
访问10.72.36.112出现转发错误,查看日志发现是302错误,并且会跳转到后端IP10.79.36.119上
现象:nginx在使用非80端口做反向代理时,浏览器访问发现返回302错误
原因:proxy.conf文件中定义的proxy_set_header Host $host;
意思是nginx接收到浏览器请求后修改请求头中的host信息,然后再把请求转发给后端真实服务节点,服务节点响应后把返回信息传送给nginx,而由于nginx是使用的非80端口做代理,后端服务节点却依然以为nginx是80端口,所以响应信息没有正确的返回给nginx的非80端口
解决:修改为proxy_set_header Host $host:$server_port;即可,这样就把请求头中的host修改为nginx的非80端口了,后端服务节点就知道响应应该返回的正确nginx代理端口
转自
nginx做反向代理时出现302错误-月满轩尼诗-51CTO博客
http://blog.51cto.com/sunnyyu/1384417
增加proxy_set_header Host $host:$server_port,但是报错提示proxy_hide_header Server所在行报错
location / { proxy_pass http://jboss; #proxy_set_header Host $host; proxy_set_header Host $host:$server_port proxy_hide_header Server; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; }
[root@hchtest4 conf]# service nginx reload nginx: [emerg] invalid number of arguments in "proxy_set_header" directive in /usr/local/nginx/conf/nginx.conf:64 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
最后发现是因为proxy_set_header Host $host:$server_port后面灭有加分号;
或者使用以下这个方法
添加 proxy_set_header X-Real-PORT $remote_port;这个参数
,其实这种方法成功的原因不是因为添加了 $remote_port参数,而是proxy_set_header Host $host:$server_port;加了分号
location / {
proxy_pass http://jboss;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
使用以下方法,测试结果不行,无法访问。
把proxy_set_header Host $host:$server_port;改成proxy_set_header Host $host:$proxy_port;
服务器名称和端口一起通过代理服务器传递。
proxy_set_header Host $host:$proxy_port;
location / {
proxy_pass http://jboss;
#proxy_set_header Host $host;
proxy_set_header Host $host:$proxy_port;
proxy_hide_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
转自
Nginx proxy_set_header 理解 - 简书
https://www.jianshu.com/p/cc5167032525
proxy_set_header设置Host为$proxy_host,$host与$local_host的区别
proxy_set_header设置Host为$proxy_host,$host与$local_host的区别 - CSDN博客
http://blog.csdn.net/a19860903/article/details/49914131
nginx中proxy_set_header Host $host;的作用!~请详解!~_百度知道
https://zhidao.baidu.com/question/430762587.html
Nginx proxy_set_header中$proxy_host,$host,$http_host的区别 - CSDN博客
http://blog.csdn.net/u011897301/article/details/72486278
【转】nginx中proxy_set_header Host $host的作用 - yanghj - 博客园
https://www.cnblogs.com/yanghj010/p/5980974.html
关于nginx中proxy_set_header的设置 - CSDN博客 http://blog.csdn.net/felix_yujing/article/details/51682655
nginx 做proxy 不转发 http header问题解决 - CSDN博客 http://blog.csdn.net/wx_mdq/article/details/10466891
获取真实客户端IP
参考
nginx代理tomcat不能获取真实ip地址解决方法 - CSDN博客
http://blog.csdn.net/white__cat/article/details/51513664
nginx获取客户端IP实现 - CSDN博客
http://blog.csdn.net/zjin_hua/article/details/52073602
nginx参数proxy_redirect说明
参考
关于nginx参数proxy_redirect的设置 - bjsunwei的博客 - CSDN博客
http://blog.csdn.net/bjsunwei/article/details/73481359
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?