docker for mac/windows 里的 nginx 如何获取到访客的真实 IP?

2017-06-23 22:37:57 +08:00
 fuxkcsdn

docker 版本: Version 17.03.1-ce-mac12 (17661)

nginx 镜像版本 latest(1.13.1)

宿主机内网 IP:192.168.1.101

手机内网 IP:192.168.1.103

> docker run -d -p 8080:80 --name httpd nginx:latest

在宿主机上访问 http://192.168.1.101:8080

在手机上访问 http://192.168.1.101:8080

> docker logs httpd
172.17.0.1 - - [23/Jun/2017:14:36:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.51.0" "-"
172.17.0.1 - - [23/Jun/2017:14:36:41 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 ..." "-"

同样的命令在 linux 宿主机上不会出现这问题

5431 次点击
所在节点    Docker
5 条回复
neilp
2017-06-23 22:47:22 +08:00
楼主是用的 docker on Windows 10 吗

我之前装这个后, 貌似主机的 tracert 就无法工作了. 麻烦楼主在主机上试试 tracert

```
tracert www.baidu.com

```
htfy96
2017-06-23 22:59:37 +08:00
docker 做了 SNAT,最简单的做法是 docker run -d -p 8080:80 --net host --name httpd nginx:latest
fuxkcsdn
2017-06-23 23:51:49 +08:00
@neilp 安装的是 docker for windows,主机系统是 windows 10,刚测试 主机的 tracert 是正常的

@htfy96 用 --net host 后访问不了呢...
> docker run -d -p 8080:80 --net host --name httpd nginx:latest
7dfdffecfaa61b22a0a0113cd461a2414cacf3c87cea437cba833d62281b7228
> curl 127.0.0.1
curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused
> curl 127.0.0.1:8080
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
> curl 192.168.1.101:8080
curl: (7) Failed to connect to 192.168.1.101 port 8080: Connection refused
> curl 192.168.1.101:80
curl: (7) Failed to connect to 192.168.1.101 port 80: Connection refused
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7dfdffecfaa6 nginx:latest "nginx -g 'daemon ..." 22 seconds ago Up 21 seconds httpd
xoxo419
2018-09-10 11:29:31 +08:00
哈喽, 这个问题现在有解决的方法了么
xoxo419
2018-09-15 15:15:56 +08:00
MAC- 只能获取容器的 IP. 但是 Linux 上可以获取到客户端的真实 IP [关于问题相关的资料]( http://www.majianwei.com/archives/7909)

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

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

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

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

© 2021 V2EX