Docker with Symfony an Nginx how to get real ip address?

Viewed 1822

I use docker with nginx and symfony applicaiton and my goal get real client ip. At this moment I got internal docker ip like 172.23.*.*. Looks like very standart case, but I faced with trouble about this. Could you help me please how to resolve it ? In global _SERVER variable I get

SERVER_ADDR = "172.23.0.7"
REMOTE_PORT = "40046"
REMOTE_ADDR = "172.23.0.1"

based on this I get in Request $request $request->getClientIp() = 172.23.0.1, but it's not real ip from user (it should be 212.55.92.12)

my nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log off;
  error_log off;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
  open_file_cache max=100;
  client_body_temp_path /tmp 1 2;
  client_body_buffer_size 256k;
  client_body_in_file_only off;
}

daemon off;

my host.conf: I added in set_real_ip_from ip from ifconfig, but it's not help and I did not get header X-Forwarded-For, where is it could be ?

headers from request http foundation

connection = {array} [1]
accept-encoding = {array} [1]
host = {array} [1]
postman-token = {array} [1]
cache-control = {array} [1]
accept = {array} [1]
user-agent = {array} [1]
content-length = {array} [1]
content-type = {array} [1]
x-php-ob-level = {array} [1]

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:9c:d9:03:ca  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

server {
    server_name symfony.localhost;
    root /var/www/symfony/public;
    # issue with ip and the nginx proxy
    real_ip_header X-Forwarded-For;
    set_real_ip_from 172.17.0.1/16;


    location / {
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /index.php/$1 last;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    error_log /var/log/nginx/symfony_error.log;
    access_log /var/log/nginx/symfony_access.log;
}

my docker-compose.yml

version: '3.7'
services: 
    php:
        container_name: php-fpm
        build: ./php-fpm
        ports:
            - "9000:9001"
        volumes:
            - ./symfony:/var/www/symfony:cached
            - ./logs/symfony:/var/www/symfony/var/log:cached
        depends_on: 
            - db
        networks:
            - php

    nginx:
        container_name: nginx
        build: ./nginx
        ports:
            - mode: host
              protocol: tcp
              published: 80
              target: 80
            - mode: host
              protocol: tcp
              published: 443
              target: 81
        depends_on: 
            - php
        networks:
            - php
        volumes:
            - ./logs/nginx:/var/log/nginx:cached
            - ./symfony:/var/www/symfony:cached
1 Answers

If you use reverse proxy, you need to tell this to Symfony.

For the Symfony Flex, change the TRUSTED_PROXIES variable in your .env file. For others Symfony versions, you can:

// public/index.php

// ...
$request = Request::createFromGlobals();

// tell Symfony about your reverse proxy
Request::setTrustedProxies(
    // the IP address (or range) of your proxy
    ['192.0.0.1', '10.0.0.0/8'],

    // trust *all* "X-Forwarded-*" headers
    Request::HEADER_X_FORWARDED_ALL

    // or, if your proxy instead uses the "Forwarded" header
    // Request::HEADER_FORWARDED

    // or, if you're using AWS ELB
    // Request::HEADER_X_FORWARDED_AWS_ELB
);

For more details, check this: https://symfony.com/doc/current/deployment/proxies.html.

Regards!

Related