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

  docker, laravel-8, nginx, nginx-reverse-proxy, php

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
[REMOTE_PORT] => 55378
[REMOTE_ADDR] => 172.20.0.1
[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;
    }
}

Source: Docker Questions

LEAVE A COMMENT