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

2016-10-11 13:27:13 +08:00
 yanest
nginx 是 192.168.101.x/24 ,后端服务器在 192.168.10.x/24 , 这时候如果使用 proxy_pass 转发到后端会出错,该如何处理。
3028 次点击
所在节点    NGINX
12 条回复
hxsf
2016-10-11 13:38:55 +08:00
... 确保两个能连通就行
谁说一定要在同一子网内的
walkman660
2016-10-11 14:39:09 +08:00
2 台服务器不通的话,怎么写都没用
2 台服务器通的话,官方文档说怎么写就怎么写
Ellison
2016-10-11 14:53:06 +08:00
这应该去找你们网管
ifaii
2016-10-11 14:53:40 +08:00
有正确的路由 目标可达就可以了,跟在哪个子网没有关系
youyoumarco
2016-10-11 15:20:03 +08:00
网路通就可以了
yanest
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
2016-10-11 15:33:57 +08:00
现在访问 brandon.yunmt.net ,浏览器返回的是内网地址
surfire91
2016-10-11 15:40:33 +08:00
楼主说会出错,倒是说一下出了什么错呗
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
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
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
2016-10-11 16:07:37 +08:00
@Ellison 的确是 proxy_redirect 的问题。我对 nginx 了解的不透。

@bwangel docker 不清楚,如果是在一个三层网络下,就会有这问题。不过已经找到原因了,多谢。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/311870

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX