D
D
Dmitry Tarasov2020-05-10 15:12:35
Docker
Dmitry Tarasov, 2020-05-10 15:12:35

How to get the user's real IP in Docker + Nginx + PHP-Fpm?

Hello! How to get the user's real ip address?
In php scripts and nginx logs, it gives me 192.168.16.1 , and on the Internet, my ip is 46.216.69.12, how can I make sure that the address that is visible on the Internet is transmitted to nginx and php?

docker-compose

version: '3.7'
services:
    php:
      depends_on:
        - db
      build:
        context: .
        dockerfile: docker/php/Dockerfile
      volumes:
        - './app:/var/www/app'
        - ./docker/logs/php:/var/log

    nginx:
        image: nginx:1.18.0-alpine
        depends_on:
          - db
        restart: always
        ports:
          - 80:80
        volumes:
          - './app:/var/www/app'
          - ./docker/nginx/sites/local.conf:/etc/nginx/conf.d/default.conf
          - ./docker/nginx/local.default.conf:/etc/nginx/local.default.conf
          - ./docker/logs/nginx:/var/log/nginx
#        environment:
#          - NGINX_HOST=test-doc-fast.loc

    db:
      image: mysql:latest
      ports:
        - '${DB_PORT}:3306'
      command: '--character-set-server=utf8 --collation-server=utf8_general_ci'
      environment:
        MYSQL_ROOT_PASSWORD: '${MYSQL_ROOT_PASSWORD}'
        MYSQL_DATABASE: '${MYSQL_DATABASE}'
        MYSQL_USER: '${MYSQL_USER}'
        MYSQL_PASSWORD: '${MYSQL_PASSWORD}'
      volumes:
        - './docker/mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d'
        - './docker/mysql/db_data:/var/lib/mysql'

    profit-redis:
      image: redis:latest

networks:
  default:
    external:
      name: profit_network


nginx conf

listen 80;
index index.php index.html;
error_log  /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;

try_files  $uri $uri/ @rewrite;

location @rewrite {
  rewrite  ^/(.*)$ /index.php?pg=$1;
}

if ($request_uri ~ ^/index) {
  rewrite ^.*$ / permanent;
}

location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|js|css|txt|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar)$ {
  access_log off;
  expires 7d;
  break;
}


#location / {
#    try_files $uri $uri/ /index.php$is_args$args;
#}

location ~ \.php$ {
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' $http_origin;
        add_header 'Access-Control-Allow-Methods' 'GET, PUT, HEAD, POST, OPTIONS';

        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Type,Content-Length,Content-Range';

        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' '0';
        return 204;
    }

    if ($request_method = "POST") {
        add_header "Access-Control-Allow-Origin" "*";
        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
        add_header "Access-Control-Allow-Headers" "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
        add_header "Access-Control-Expose-Headers" "Content-Length,Content-Range";
    }

    if ($request_method = "GET") {
        add_header "Access-Control-Allow-Origin" "*";
        add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
        add_header "Access-Control-Allow-Headers" "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range";
        add_header "Access-Control-Expose-Headers" "Content-Length,Content-Range";
    }
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2020-05-10
@deepblack

Look here => https://github.com/nginx-proxy/nginx-proxy/issues/130

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question