当前位置首页 > Nginx知识

nginx在使用非80端口做反向代理【转】

阅读次数:217 次  来源:admin  发布时间:

设置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代理端口

转自

ginx做反向代理时出现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 / {

roxy_pass http://jbo

roxy_set_header Host $host:$server_port

roxy_set_header X-Real-IP $remote_addr

roxy_set_header X-Real-PORT $remote_port

roxy_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

roxy_set_header设置Host为$proxy_host,$host与$local_host的区别

roxy_set_header设置Host为$proxy_host,$host与$local_host的区别 - CSDN博客

http://blog.csdn.net/a19860903/article/details/49914131

ginx中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

上一篇:【转载】Centos7中使用Supervisor守护进程
下一篇:恋爱Linux(Fedora20)2——安装Java运行环境(JDK)