Environment:
2 Apache web servers (on Debian 9) behind an nginx load balancer (on CentOS 6.10)
Configuration:
The configuration below allows the nginx to load balance between the two Apache web servers. Session persistence is done via the ip_hash directive on the nginx.
The X-Forwarded is also set on the nginx and Apache is configured such that the Apache logs will have the real client public IP addresses instead of the nginx IP address.
nginx.conf
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
# server directive below are the web server IP's
upstream itproject {
ip_hash;
server x.x.x.x;
server x.x.x.x;
}
# server_name is the publicly accessible FQDN or IP
server {
listen 80;
server_name x.x.x.x;
location / {
proxy_pass http://itproject;
}
}
apache.conf
#LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
On the web servers do:
sudo apt-get install libapache2-mod-rpafsudo systemctl restart apache2