I want to know what my client's IP address is when using Elastic Load Balancing (ELB). How can I configure my Apache server running on an Ubuntu 16.04 instance to log my client’s public IP addresses behind the load balancer?

You can configure your Apache2 Web Server with these instructions.

1. Install Apache2 on an Ubuntu 16.04 instance.

sudo apt-get install apache2 -y

2. Install mod_remoteip on the Ubuntu instance.

sudo a2enmod remoteip

3. Activate the configuration by restarting Apache2.

sudo service apache2 restart

4. Edit the Apache configuration located at /etc/apache2/apache2.conf for Ubuntu as follows:

sudo vim /etc/apache2/apache2.conf

        Add this line to the configuration file.

RemoteIPHeader X-Forwarded-For

        Find the matching section that begins with LogFormat. Change this line:

LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

        To this: (Replace %h with %a in the configuration file)

LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

5. To apply the changes, save the configuration file and then restart Apache2 on your Ubuntu instance.

sudo systemctl restart apache2.service

6. Verify that the client IP is getting logged using this command on your Ubuntu instance and make a request from another instance:

tail -f /var/log/apache2/access.log

        Request from another client machine/instance:

curl -vo /dev/null http://<elb_dns_name>

7. Verify that the client public IP or Elastic IP is shown in the access.log file.

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2016-12-07

Updated: 2018-04-25