Load Balancer $_SERVER['REMOTE_ADDR'] Not working

喜夏-厌秋 提交于 2019-12-10 13:55:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!