我有一个包含nginx和PHP的堆栈,可以在Docker Swarm Cluster上运行。
在我的PHP应用程序中,我需要获取remote_addr ($_SERVER‘’REMOTE_ADDR‘),它包含访问我的webapp的客户端主机的真实IP。
但是问题是IP是由docker swarm集群通知给nginx的。它显示了一个内部IP,如10.255.0.2,但真正的IP是来自客户端主机的外部IP (如192.168.101.151)。
我该怎么解决这个问题呢?
我的docker-compose文件:
version: '3'
services:
php:
image: php:5.6
volumes:
- /var/www/:/var/www/
- ./data/log/php:/var/log/php5
networks:
- backend
deploy:
replicas: 1
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- /var/www/:/var/www/
- ./data/log/nginx:/var/log/nginx
networks:
- backend
networks:
backend:我的default.conf (vhost.conf)文件:
server {
listen 80;
root /var/www;
index index.html index.htm index.php;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log error;
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;
try_files $uri $uri/ /index.php;
}
location = /50x.html {
root /var/www;
}
# set expiration of assets to MAX for caching
location ~* \.(js|css|gif|png|jp?g|pdf|xml|oga|ogg|m4a|ogv|mp4|m4v|webm|svg|svgz|eot|ttf|otf|woff|ico|webp|appcache|manifest|htc|crx|oex|xpi|safariextz|vcf)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_read_timeout 300;
}
}我的nginx配置文件:
user nginx;
worker_processes 3;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
keepalive_timeout 15;
client_body_buffer_size 100K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 2 1k;
gzip on;
gzip_comp_level 2;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/x-javascript text/xml text/css application/xml;
log_format main '$remote_addr - $remote_user [$time_local] "$request_filename" "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
include /etc/nginx/conf.d/*.conf;
}发布于 2018-05-30 04:32:00
对于那些不想读取所有github线程( https://github.com/moby/moby/issues/25526 )的人来说,对我来说最好的答案是将配置更改为:
version: '3.7'
services:
nginx:
ports:
- mode: host
protocol: tcp
published: 80
target: 80
- mode: host
protocol: tcp
published: 443
target: 81这仍然允许内部覆盖网络工作,但使用iptables的一些技巧将这些端口直接转发到容器,因此容器内的服务可以看到数据包的正确源IP地址。
iptables中没有允许在多个容器之间平衡端口的功能,因此您只能将一个端口分配给一个容器(其中包括容器的多个副本)。
发布于 2018-03-23 11:42:07
您还不能通过覆盖网络获得此功能。如果是you scroll up from bottom on this long-running GitHub issue,你会看到一些在Swarm中使用网桥网络的选项,现在你的代理可以绕过这个问题。
发布于 2020-03-27 00:22:54
将端口绑定模式更改为主机对我有效
ports:
- mode: host
protocol: tcp
published: 8082
target: 80但是,您的web前端必须侦听集群内的特定主机,即
deploy:
placement:
constraints:
[node.role == manager]https://stackoverflow.com/questions/49415595
复制相似问题