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

<p>I have set up TrustProxies.php like this:</p>

<pre><code>&lt;?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;
}
</code></pre>

<p>I have also tried</p>

<pre><code>protected $proxies = '**';
</code></pre>

<p>And</p>

<pre><code>protected $proxies = ['loadbalancer_ip_here'];
</code></pre>

<p>No matter what I have tried, it will always return load balancer ip.</p>

<p>Might this be caused by Nginx configuration? Help appreciated.</p>

<p>You have to set traffic policy in your nginx service </p>

<pre><code>externalTrafficPolicy: "Local"
</code></pre>

<p>and also </p>

<pre><code>healthCheckNodePort: "numeric port number for the service"
</code></pre>

<p>More details in <a href="https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/#preserving-the-client-source-ip" rel="nofollow noreferrer">Preserving the client source IP</a> doc</p>
