简体   繁体   中英

Getting a user's IP address in Laravel through Docker containers and Nginx webserver

Good day,

My problem is that I am ONLY getting my Docker network IP address when using Laravel's $request->ip() or $request->getClientIp () or $request->server->get('REMOTE_ADDR') or $_SERVER['REMOTE_ADDR'] which essentially all do the same I assume. So far the problem occurs on my local test pc (without Nginx Reverse Proxy) as well as on my live servers

I hope I've supplied enough relevant info below, if not please let me know:) Thanks in advance!

So my setup is the following (simplified), both Nginx configs are pasted below as well:

Server -> Nginx Reverse Proxy -> Docker Containers & Network (db, php, nginx, redis) -> Nginx Webserver <-> PHP-FPM

Some results related to _SERVER:

[SERVER_ADDR] => 172.20.0.3 (nginx webserver)
[REMOTE_PORT] => 55378
[REMOTE_ADDR] => 172.20.0.1 (docker network / gateway)
[SERVER_SOFTWARE] => nginx/1.19.6

I have read multiple forums and solutions for hours and hours while trying and changing configs etc. but none of it seems to be working.

What I have tried:

So I have also tried to set my "Trusted Proxy" in Laravel's /Http/Middleware/TrustProxies.php by allowing all: protected $proxies = '*';

I also don't need these additional Laravel packages as of >5.5 I believe this is a built in feature of Laravel (TrustProxies).

Please find below my Nginx webserver config :

server {
    listen 80;
    server_name domain.com;
    return 301 https://www.example.com;
}

server {
    listen 80 default_server;

    index index.html index.htm index.php;

    server_name www.example.com;

    root /var/www/public;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-fpm:9013;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    
   # Support Clean (aka Search Engine Friendly) URLs & enable Gzip compression:
    location / {
        proxy_set_header    X-Real-IP            $remote_addr;
        proxy_set_header    X-Forwarded-For      $proxy_add_x_forwarded_for;
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
   # Override the load balancer IP with real IP.
    fastcgi_param REMOTE_ADDR $http_x_real_ip;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

And my server's Nginx Reverse Proxy config (IF relevant):

server {
    listen 80;
    listen [::]:80;

    server_name     example.com.au www.example.com.au;

    location / {
        proxy_pass      http://localhost:8013;
        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-Host $server_name;
    }
}

Ok figured it out: :D

  1. I was using Cloudflare as a proxy, my DNS is with CloudFlare and I have enabled the "Proxy through CloudFlare" option due to speed / caching improvements. This resulted in CloudFlare "injecting" a HTTP_CF_CONNECTING_IP into my header. To retrieve this header value I have used $_SERVER['HTTP_CF_CONNECTING_IP'] in my code.

  2. Another solution was to use the HTTP_X_FORWARDED_FOR value from the header by using _SERVER['HTTP_X_FORWARDED_FOR'] , this I believe would, in my situation only work after doing the following (posted in my question):

  • TrustProxies.php
  • proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

I hope this helps others as well:D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM