Search This Blog

Wednesday, November 21, 2018

How to get original client IP address for requests from Apache

Sometimes site visitors or misbehaving crawlers need to be blocked based on their IP address. PageSpeed Service from Google serves your webpages and so all requests to your origin server will be from Google's IP addresses.  Similar situation applies to Amazon load-balancer with Amazon EC2 instances. To get the IP address of the user on your origin server, use the value stored in the X-Forwarded-For HTTP header.

In CentOS, in file /etc/httpd/conf/httpd.conf, the client IP address can be obtained within your configuration file using the %{X-Forwarded-For}i variable. If you don't care about recording PageSpeed server IP addresses, you can simply replace the traditional %h with %{X-Forwarded-For}i in the format string of your server logs.

It uses "combined" format by default.