Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Balancer $_SERVER['REMOTE_ADDR'] Not working

I've switched to using an Amazon AWS Elastic Load Balancer and now I'm getting a private IP address for $_SERVER['REMOTE_ADDR'] - which used to give me the client's IP address.

How do I get the clients IP address now?

like image 373
Amy Neville Avatar asked Apr 03 '16 19:04

Amy Neville


2 Answers

Your webserver receives HTTP requests from the Amazon ELB. Therefore, the remote address will always be one of the ELB ip addresses. If you need the remote host's address from behind the ELB, get it from HTTP header "X-Forwarded-For".

like image 170
Bert Jan Schrijver Avatar answered Sep 20 '22 12:09

Bert Jan Schrijver


The below works for me in httpd.conf global section where the LogFormat is defined. With the change below, $_SERVER['REMOTE_ADDR'] will be set to the client ip on php-fpm and not the ELB ip.

<IfModule remoteip_module>
   RemoteIPHeader X-Forwarded-For
   LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
   LogFormat "%a %l %u %t \"%r\" %>s %b" common
</IfModule>

Solution is from https://medium.com/@jiraknet/apache-2-4-mod-remoteip-for-get-real-ip-on-aws-elb-6e9f40876b06

like image 34
aerorat Avatar answered Sep 18 '22 12:09

aerorat