两次反向代理后转向失效


在内网有一台服务器,有3个端口提供Web服务,服务器分别是nginx,apache和serv-u。现在需要把三个服务转发到外网。

  • 没有控制路由器的权限
  • 网管只用PortTunnel把80端口转发到外网882

于是就想合并到一个端口转发,使用nginx反代判断Host解决。
nginx.conf如下:


 server {
    listen       80;
    server_name  no1.home s1.abc.com;
    index index.html index.htm index.php;      
    access_log off;
    location / {
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   X-HappyHome  XMWZWX;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://10.110.39.2:10;    
    }
}

三段代码分别反代三项。用Host解析后是正常的,但是一旦用上网管设置的外网就会出现问题:

访问 http://xxx.com:882/folder
会自动跳转至 http://xxx.com:80/folder/

请问是哪里的问题?是nginx.conf配置错误了吗?

nginx_proxy nginx 反向代理

天下狼友是一家 10 years, 8 months ago

所贴出的配置文件看不到问题所在。所以只能给出如下建议(按顺序来):

  1. 打开 access_log, 确认访问 http://xxx.com:882 的请求确实已经被该nginx处理。建议查看全局的log,而不是针对此 virtual server 所配置的日志,这样用来排除由于配置错误,请求被其他 virtual server 处理的问题。
  2. 如果[1]得到确认,则如法炮制确认 http://10.110.39.2:10 是否收到请求。
  3. 如果[2]也得到了确认,那么这个时候,请对监听 10.110.39.2:10 的应用程序进行 debug。
ぁ仙逍遥ア answered 10 years, 8 months ago

好吧,我翻半天nginx的文档后找到了答案。
在三个站点的配置里面都加一句
proxy_redirect "http://10.110.39.2:80" "http://$host:882" ;
就可以了。

taema answered 10 years, 8 months ago

Your Answer