0

I'm trying to get the IP of the client that's making a registration request, but when I try to get the data from code, I get the address of the Docker container. I made the following changes:

bootstrap/app.php

    ->withMiddleware(function (Middleware $middleware) {
        $middleware->prepend(TrustProxies::class);
    })

.env

    TRUST_PROXIES=*
    TRUST_PROXIES_HEADERS=HEADER_X_FORWARDED_FOR

app.conf (Nginx)

    server {
    listen 80;
    server_name localhost;

    root /var/www/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

        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;
    }

    location ~ \.php$ {
        fastcgi_pass app-AxsSEMEFO:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        # Asegúrate de pasar los headers como variables correctas
        fastcgi_param HTTP_X_FORWARDED_FOR $proxy_add_x_forwarded_for;
        fastcgi_param HTTP_X_REAL_IP $remote_addr;
        fastcgi_param HTTP_X_FORWARDED_PROTO $scheme;
    }
}

and through this i do test

    Route::get('/debug-ip', function () {
    return response()->json([
        'Laravel IP' => request()->ip(),
        '_SERVER["HTTP_X_FORWARDED_FOR"]' => $_SERVER['HTTP_X_FORWARDED_FOR'] ?? 'not set',
        '_SERVER["X_FORWARDED_FOR"]' => $_SERVER['X_FORWARDED_FOR'] ?? 'not set',
        'headers' => request()->headers->all(),
    ]);
    });

to get this:

{
  "Laravel IP": "172.18.0.1",
  "_SERVER[\"HTTP_X_FORWARDED_FOR\"]": "172.18.0.1",
  "_SERVER[\"X_FORWARDED_FOR\"]": "not set",
  "headers": {
    "cookie": [
      "XSRF-TOKEN=eyJpdiI6IjBQYzVoRlA2VEx2bkpsV0gyZ1hlK0E9PSIsInZhbHVlIjoicklMajJtSnJ6K3ZPdi9WMXpKRzMxK2xMajBRQjByeWk0WmUrK1BBRXpaTjVSUHpnS1FyQXRvVFJKRFNiUHA3Z3BOZDFqaE1Yd2ZoeVdwbHJTZzBibTlxdjk5Z3RoU1QxUmNVOTFNQWJMcWRwWXkrUWpUVFdXazdqWnpydkErREIiLCJtYWMiOiJlMzE3MWYxODRkZTdiYmRhNGE5MDEwZDkyNDQ1MjEzNDNlYjI1YTRjMWQ2MmRhMWQ4NDhmZTUwYmZmZTFhYTdkIiwidGFnIjoiIn0%3D; laravel_session=eyJpdiI6ImRVa0V5QzZIU21Ob0lRUktiMWVsU3c9PSIsInZhbHVlIjoiMEdLNjV6d3MvNmhZUnBXanpxWTJjQitXVkNJdjdLdlZVY0NOakRoa1N1UGM0eTJkL1lxcHFITFg3WU1KRXpJWXg2TEtEdWR3QVJzbE9CZVhuVDRpSFl2cUZINnplcUwyL1E2QmlXcnJGUTFyLy84RGx2eHN3TmdScGY0SnZNZW4iLCJtYWMiOiIyNDkxZjI5ZDg5ODM3ODhlYzFhYWMyOWYxYzFkODdlMDk1NjY3Y2M0MThiYjAxOTRjODUxMmZhZTU1ODVmMjlkIiwidGFnIjoiIn0%3D"
    ],
    "accept-encoding": [
      "gzip, deflate, br, zstd"
    ],
    "sec-fetch-dest": [
      "document"
    ],
    "sec-fetch-user": [
      "?1"
    ],
    "sec-fetch-mode": [
      "navigate"
    ],
    "sec-fetch-site": [
      "none"
    ],
    "accept-language": [
      "es-419,es;q=0.9"
    ],
    "sec-gpc": [
      "1"
    ],
    "accept": [
      "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8"
    ],
    "user-agent": [
      "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"
    ],
    "upgrade-insecure-requests": [
      "1"
    ],
    "sec-ch-ua-platform": [
      "\"Windows\""
    ],
    "sec-ch-ua-mobile": [
      "?0"
    ],
    "sec-ch-ua": [
      "\"Brave\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\""
    ],
    "cache-control": [
      "max-age=0"
    ],
    "connection": [
      "keep-alive"
    ],
    "host": [
      "localhost:3020"
    ],
    "x-forwarded-proto": [
      "http"
    ],
    "x-real-ip": [
      "172.18.0.1"
    ],
    "x-forwarded-for": [
      "172.18.0.1"
    ]
  }
  }

I'm not really an expert on Docker, so any help is welcome. If you need more information, please let me know.

Thanks in advance.

2
  • This question is similar to: How to get the client IP address in PHP. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented May 30 at 22:21
  • Getting the client's IP address is notoriously unreliable as the address you see can be the address of NAT routers, caching servers, reverse proxies and other things along the route. It's possible that the web server doesn't bother to populate the fields at all. The answers to the linked question contain a number of possible techniques that may help, but I recommend that whatever you intend to do with this data, you find another way. Commented May 30 at 22:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.