Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andregumieri's avatar

Getting the right IP behind Load Balanced Elastic Beanstalk with Docker

My Laravel 6.x setup consists of a Docker, running in an Elastic Beanstalk proxying with NGinx that is behind a load balancer.

With that setup, I'm having a real hard time to get the correct IP address, which is affecting the Throttle middleware, because the IP Laravel gets is either from the host machine or from the load balancer.

So, every user is in concurrence with everyone for the limits of the throttle.

Any ideas on how to fix that?

That's a copy of $_SERVER. The correct IP, in this case, is the first one in HTTP_X_FORWARDED_FOR. Correct IP for this scenario is 188.67.242.77.

Array
(
    ...
    [HTTP_X_REAL_IP] => 172.31.28.165
    [HTTP_X_FORWARDED_FOR] => 188.67.242.77, 172.31.28.165
    [HTTP_X_FORWARDED_PROTO] => https
    [HTTP_X_FORWARDED_PORT] => 443
    ...	
    [SERVER_SOFTWARE] => Apache/2.4.38 (Debian)
    [SERVER_NAME] => {MASKED}
    [SERVER_ADDR] => 172.17.0.3
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 172.17.0.1
    ...
    [REMOTE_PORT] => 47334
    ...
    [SCRIPT_NAME] => /index.php
    [PHP_SELF] => /index.php
    [REQUEST_TIME_FLOAT] => 1584032941.764
    [REQUEST_TIME] => 1584032941
)

Tested changing everything I could imagine in TrustProxies.php. protected $headers set to Request::HEADER_X_FORWARDED_AWS_ELB, Request::HEADER_X_FORWARDED_ALL, Request::HEADER_X_FORWARDED_FOR, and protected $proxies to * and **. Mixed and matched the possibilities. No good. I got either the 172.17.0.1 (host ip) or 172.31.28.165 (load balancer ip)

Any ideas?

Thank you so much in advance

0 likes
0 replies

Please or to participate in this conversation.