docker nginx 反向代理 go 端口 反向不通,反向其它可以!

2019-09-26 21:23:01 +08:00
 coderabbit

docker 子网

172.100.0.0/24

本地宿主机 ip

//我不确定
172.100.0.1

go 本地服务及端口

127.0.0.1:9606

本地域名

www.demo.cn

nginx 反向代理

location ~ ^(/static|/service) {
    proxy_redirect off ;
    proxy_set_header Host $host;
    proxy_set_header X-Real-Ip $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 50m;
    client_body_buffer_size 256k;
    proxy_connect_timeout 30;
    proxy_send_timeout 30;
    proxy_read_timeout 60;
    proxy_buffer_size 256k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;
    proxy_temp_file_write_size 256k;
    proxy_max_temp_file_size 128m;
    proxy_pass http://172.100.0.1:9606$request_uri;
}

目前遇到的问题就是这样的,我想访问 www.demo.cn/static 转发到 go 服务。一直转发不过去 我输入

www.demo.cn/static/ 
直接显示
404 Not Found
nginx/1.17.4

我把代理地址改成百度的

proxy_pass   http://www.baidu.com;

然后访问 www.demo.cn/static 正常转发走了!

我在 docker 里 curl 缩主机 172.100.0.1 无法 curl 然后找了设置 mac 回环地址

sudo ifconfig lo0 alias 172.100.0.1/24

本地访问 172.100.0.1 正常 docker curl 172.100.0.1 但是我加上端口 curl http://172.100.0.1:9606 端口又不通。

docker ping 宿主机又能 ping 通

64 bytes from 172.100.0.1: seq=0 ttl=64 time=0.175 ms

不知道该怎么处理了,想请教下大家!

4730 次点击
所在节点    NGINX
9 条回复
coderabbit
2019-09-26 21:35:18 +08:00
docker curl 宿主机
```
curl http://172.100.0.1:9606
curl: (7) Failed to connect to 172.100.0.1 port 9606: Connection refused
/var/www/html # curl http://172.100.0.1:9606/static
curl: (7) Failed to connect to 172.100.0.1 port 9606: Connection refused
```
coderabbit
2019-09-26 21:46:47 +08:00
错误日志

```

2019/09/26 21:43:01 [error] 6#6: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.100.0.1, server: www.demo.cn, request: "GET /static HTTP/1.1", upstream: "http://172.100.0.1:9606/static", host: "www.demo.cn"
2019/09/26 21:43:01 [error] 6#6: *2 open() "/var/www/50x.html" failed (2: No such file or directory), client: 172.100.0.1, server: www.demo.cn, request: "GET /static HTTP/1.1", upstream: "http://172.100.0.1:9606/static", host: "www.demo.cn"

```
看到把 50x 了 具体也没有说 50 几。
iceheart
2019-09-26 21:51:44 +08:00
go listen 127.0.0.1 当然不行,改成 0.0.0.0
openwrt
2019-09-26 21:52:51 +08:00
你的 go 服务部署在哪,监听的是 127.0.0.1 吗,监听 127.0.0.1 用 172.x.x.x 访问当然不通
676529483
2019-09-26 21:55:37 +08:00
你把 nginx 的配置 proxy_pass http://172.100.0.1:9606$request_uri;里面的 ip 改成 127.0.0.1,再试
coderabbit
2019-09-26 22:14:27 +08:00
经自己 curl ping
curl docker.for.mac.host.internal:9606
404 page not found
已经返回 go 服务的 404 了!但是直接访问 www.demo.cn/static/index.html 还是 404 先回去再看原因

@676529483 经测试过的 不通的!
@openwrt 本地 mac 开发
@iceheart 改成 0000 不行的
mikeguan
2019-09-26 22:21:28 +08:00
怎么宿主机和 docker 一个网段?正常安装 docker 的不会选同一网段的。还不如 docker 直接用 host 网络
coderabbit
2019-09-26 23:53:55 +08:00
彻底解决问题反代正常了 感谢大家的回答!
参考: https://docs.docker.com/docker-for-mac/networking/#/known-limitations-use-cases-and-workarounds
https://stackoverflow.com/questions/35744650/docker-network-nginx-resolver

http {
resolver 127.0.0.11 ipv6=off;
}

resolver 127.0.0.11 ipv6=off; 很重要 且 ip 不能变!

location ~ ^(/static|/service) {
proxy_pass "http://host.docker.internal:9606$request_uri";
}

http://host.docker.internal 这个地址是 dcoker 访问宿主机地址 https://docs.docker.com/docker-for-mac/networking/#/known-limitations-use-cases-and-workarounds 这里已经说的很清楚了! 之前我把 proxy_pass 设置成 http://host.docker.internal 代理依然不通!
一直报 no resolver defined to resolve host.docker.internal, client: 172.100.0.1,
然后去 stackoverflow 找到了答案 resolver 127.0.0.11 ipv6=off;设置好重启 nginx 反代顺利通过!
mchl
2019-09-27 06:26:59 +08:00
This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac.

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

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

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

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

© 2021 V2EX