我在用户创建的覆盖网络内的 Docker Swarm 中将 nginx 容器作为服务运行。两者都使用:
docker network create --driver overlay proxy
docker service create --name proxy --network proxy -p 80:80 nginx
通过浏览器访问 nginx 站点时,在 nginx 访问日志中,远程地址记录为 10.255... 格式化地址,我假设是 Swarm 负载均衡器地址。问题是如何知道/记录访问站点的最终客户端的地址,而不是负载均衡器的地址。
最佳答案
很好!,大多数人分析 nginx access.log
和客户端 ip 是其中的重要部分。
作为 docker 版本 1.12.1 存在问题。 nginx 将记录 swarm 覆盖 ip。但是客户端 ip 可以作为独立容器记录。作为一种解决方法,您可以有一个指向 swarm 服务的 reverse proxy
。我知道这与 swarm 的 Highavablity
和 Self Healing
概念背道而驰,但似乎是目前唯一的解决方法。
示例配置:(假设 swarm 服务正在监听 localhost 上的 8081)
server {
listen 80 default_server;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8181;
proxy_read_timeout 90;
}
}
更多信息请访问 this github issue .
另一种选择:
您可以在主机模式下使用网络。
docker service create \
--name nginx \
--network <your overlay network> \
--publish mode=host,target=80,published=80 \
--publish mode=host,target=443,published=443 \
--replicas 1 \
nginx
关于nginx - 在 Docker Swarm 1.12 访问服务时记录客户端的 "real"IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854682/