问题
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?
回答1:
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".
回答2:
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
来源:https://stackoverflow.com/questions/36389986/load-balancer-serverremote-addr-not-working