首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker Swarm在Nginx获取真实IP (客户端主机)

Docker Swarm在Nginx获取真实IP (客户端主机)
EN

Stack Overflow用户
提问于 2018-03-22 04:04:30
回答 4查看 14.8K关注 0票数 13

我有一个包含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文件:

代码语言:javascript
复制
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)文件:

代码语言:javascript
复制
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配置文件:

代码语言:javascript
复制
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;
}
EN

回答 4

Stack Overflow用户

发布于 2018-05-30 04:32:00

对于那些不想读取所有github线程( https://github.com/moby/moby/issues/25526 )的人来说,对我来说最好的答案是将配置更改为:

代码语言:javascript
复制
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中没有允许在多个容器之间平衡端口的功能,因此您只能将一个端口分配给一个容器(其中包括容器的多个副本)。

票数 16
EN

Stack Overflow用户

发布于 2018-03-23 11:42:07

您还不能通过覆盖网络获得此功能。如果是you scroll up from bottom on this long-running GitHub issue,你会看到一些在Swarm中使用网桥网络的选项,现在你的代理可以绕过这个问题。

票数 3
EN

Stack Overflow用户

发布于 2020-03-27 00:22:54

将端口绑定模式更改为主机对我有效

代码语言:javascript
复制
ports: 
  - mode: host 
    protocol: tcp 
    published: 8082 
    target: 80

但是,您的web前端必须侦听集群内的特定主机,即

代码语言:javascript
复制
deploy: 
  placement: 
    constraints: 
      [node.role == manager]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49415595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档