V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
yanest
V2EX  ›  NGINX

如果后端与 Nginx 不在同一子网下, proxy_pass 该如何使用?

  •  
  •   yanest · 2016-10-11 13:27:13 +08:00 · 3013 次点击
    这是一个创建于 2758 天前的主题,其中的信息可能已经有所发展或是发生改变。
    nginx 是 192.168.101.x/24 ,后端服务器在 192.168.10.x/24 , 这时候如果使用 proxy_pass 转发到后端会出错,该如何处理。
    12 条回复    2016-10-11 16:07:37 +08:00
    hxsf
        1
    hxsf  
       2016-10-11 13:38:55 +08:00
    ... 确保两个能连通就行
    谁说一定要在同一子网内的
    walkman660
        2
    walkman660  
       2016-10-11 14:39:09 +08:00
    2 台服务器不通的话,怎么写都没用
    2 台服务器通的话,官方文档说怎么写就怎么写
    Ellison
        3
    Ellison  
       2016-10-11 14:53:06 +08:00
    这应该去找你们网管
    ifaii
        4
    ifaii  
       2016-10-11 14:53:40 +08:00 via iPhone
    有正确的路由 目标可达就可以了,跟在哪个子网没有关系
    youyoumarco
        5
    youyoumarco  
       2016-10-11 15:20:03 +08:00
    网路通就可以了
    yanest
        6
    yanest  
    OP
       2016-10-11 15:24:40 +08:00
    @hxsf
    @walkman660
    @Ellison
    @ifaii
    @youyoumarco
    大哥们,网络当然通,我没说过不通吧,真实醉了。

    server {
    listen 80;
    server_name brandon.yunmt.net;
    proxy_redirect off;
    location / {
    proxy_http_version 1.1;
    proxy_pass http://192.168.10.72:8099;
    proxy_buffering off;
    client_max_body_size 0;
    proxy_connect_timeout 3600s;
    proxy_read_timeout 3600s;
    proxy_send_timeout 3600s;
    send_timeout 3600s;
    }
    }
    yanest
        7
    yanest  
    OP
       2016-10-11 15:33:57 +08:00
    现在访问 brandon.yunmt.net ,浏览器返回的是内网地址
    surfire91
        8
    surfire91  
       2016-10-11 15:40:33 +08:00
    楼主说会出错,倒是说一下出了什么错呗
    Ellison
        9
    Ellison  
       2016-10-11 15:44:04 +08:00
    @yanest 又不贴配置,又不贴日志,谁知道你是什么错误?
    问题出在 proxy_redirect off
    文档说的很清楚
    http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
    toposort
        10
    toposort  
       2016-10-11 15:47:00 +08:00
    @yanest 你现在的错误是访问后打开的是一个内网地址么?
    $curl "brandon.yunmt.net" -I
    HTTP/1.1 302 Found
    Server: nginx/1.6.2
    Date: Tue, 11 Oct 2016 07:45:33 GMT
    Content-Type: text/html;charset=UTF-8
    Content-Length: 0
    Connection: keep-alive
    Location: http://192.168.10.72:8099/seeyon/index.jsp

    所以你的错误跟 proxy_pass 怎么写没关系啊,看下你的后端逻辑
    bwangel
        11
    bwangel  
       2016-10-11 15:47:51 +08:00
    请问你的返回内网地址是什么意思,我用 docker 试了一下,完全可以啊:

    ```
    server {
    listen 80;
    server_name zvv2x;
    proxy_redirect off;
    location / {
    proxy_http_version 1.1;
    proxy_pass http://172.17.0.1:8000;
    proxy_buffering off;
    client_max_body_size 0;
    proxy_connect_timeout 3600s;
    proxy_read_timeout 3600s;
    proxy_send_timeout 3600s;
    send_timeout 3600s;
    }
    }
    ```


    其中那个 zvv2x 是我的主机名,相当于 localhost , 172.17.0.1 是 docker 容器地址。

    响应头是这样的:

    HTTP/1.1 200 OK
    Server: nginx/1.10.1
    Date: Tue, 11 Oct 2016 07:45:31 GMT
    Content-Type: text/html; charset=ANSI_X3.4-1968
    Transfer-Encoding: chunked
    Connection: keep-alive
    Content-Encoding: gzip
    yanest
        12
    yanest  
    OP
       2016-10-11 16:07:37 +08:00
    @Ellison 的确是 proxy_redirect 的问题。我对 nginx 了解的不透。

    @bwangel docker 不清楚,如果是在一个三层网络下,就会有这问题。不过已经找到原因了,多谢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2484 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:08 · PVG 23:08 · LAX 08:08 · JFK 11:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.