簡體   English   中英

Docker Swarm 在 Nginx 中獲取真實 IP(客戶端主機)

[英]Docker Swarm get real IP (client host) in Nginx

我有一個 nginx 和 PHP 堆棧可以在 Docker Swarm 集群上運行。

在我的 PHP 應用程序中,我需要從訪問我的 web 應用程序的客戶端主機獲取包含真實 IP 的 remote_addr ($_SERVER['REMOTE_ADDR'])。

但問題是 docker swarm 集群通知 nginx 的 IP。 它顯示了一個內部 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;
}

對於那些不想閱讀所有 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 中沒有允許在多個容器之間平衡端口的功能,因此您只能將一個端口分配給一個容器(其中包括一個容器的多個副本)。

您還無法通過覆蓋網絡獲得此信息。 如果你在這個長期運行的 GitHub 問題上從底部向上滾動,你會看到一些在 Swarm 中使用橋接網絡和你的代理來解決這個問題的選項。

將端口綁定模式更改為主機對我有用

ports: 
  - mode: host 
    protocol: tcp 
    published: 8082 
    target: 80

但是,您的 Web 前端必須偵聽 swarm 集群內的特定主機,即

deploy: 
  placement: 
    constraints: 
      [node.role == manager]

X-Real-IP將是直通的,您可以使用它來訪問客戶端 IP。 可以參考http://dequn.github.io/2019/06/22/docker-web-get-real-client-ip/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM