Google Cloud Kubernetes Laravel Get Visitor IP

5/29/2019

I'm trying to get visitor IP on my Laravel application that uses Nginx on Google Cloud Kubernetes Engine, under load balancer.

I have set up TrustProxies.php like this:

<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies = '*';

    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL;
}

I have also tried

protected $proxies = '**';

And

protected $proxies = ['loadbalancer_ip_here'];

No matter what I have tried, it will always return load balancer ip.

Might this be caused by Nginx configuration? Help appreciated.

-- plexcell
google-cloud-platform
ip
kubernetes
laravel
nginx

1 Answer

5/29/2019

You have to set traffic policy in your nginx service

externalTrafficPolicy: "Local"

and also

healthCheckNodePort: "numeric port number for the service"

More details in Preserving the client source IP doc

-- A_Suh
Source: StackOverflow