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

原文地址:https://www.cnblogs.com/paul8339/p/8231153.html