Setup Apache server to get Original Client IP from Node Balanacer
I am using apache server behind the Load Balancer and i want to get Original CLient Ip instead of Node Balancer IP.
I installed mod_rpaf module from below link on apache server.
I am using below configuration you can see.
ago Hello,
Thanks for your update.
But in all of the link, we have the same thing which I have applied already.
let me explain you my scenario.
I am using Apache 2.4 and i have installed the mod_rpaf module on the server and this is installed properly as below is the module details you can see.
[root@server httpd]# httpd -M | grep rpaf
rpaf_module (shared)
So the same below is the configuration file.
RPAF_ProxyIPs 192.168.255.57 96.126.119.115
RPAF_Header X-Forwarded-For
RPAF_SetHostName On
RPAF_SetHTTPS On
RPAF_SetPort On
In this i have used IP of NodeBalancer and IP which i am getting in access log it may be load balancer private IP in the field " RPAF_ProxyIPs "
In httpd.conf file i am using below LogFormat
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\ " combine
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\"" combined-forwarded
I have defined below in httpd.conf as well.
RemoteIPHeader X-Forwarded-For
–-
After all this, in my virtualHost configuration file, I used below to get the IP in access logs.
CustomLog "logs/sslmcaprotoolsaccess_log" combined-forwarded
But i am not getting client's actual IP as you can see below from access log.
192.168.255.57 - - [30/Sep/2017:02:46:49 -0400] "GET /team6fig/?s1=Maaron95 HTTP/1.0" 200 107763 "
–------
It looks like i need to setup Proxy in httpd.conf so can anybody give me configuration block for the same as this is written in below link "
Will be waiting for resolution on this.
Thanks
Nitesh
15 Replies
In apache, load the remoteip module and configure the correct header (X-Forwarded-For in my case). All can be done in a single config file:
/etc/httpd/conf.d/remoteip.conf
LoadModule remoteip_module modules/mod_remoteip.so
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 192.168.0.0/16
change the proxy IP address to be whatever is the netmask/network or single IP of your haproxy or loadbalancer.
I disabled the modrpaf module on the server and enabled the modremoteip module
Check below.
[root@mcaserver1 conf.d]# httpd -M | grep remote
remoteip_module (shared)
i created file "/etc/httpd/conf.d/remoteip.conf" and add below entry in that.
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 192.168.255.0/24
Below is my custom log file.
LogFormat "%v:%p %{X-Forwarded-For}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
using this in virtualhost like this "CustomLog "logs/sslaccesslog" vhost_combined" and i am getting below log.
websitename.com:443 - - - [09/Oct/2017:05:22:30 +0000] "POST /wp-admin/admin-ajax.php HTTP/1.1" 200 548 "
It looks like data from X-forward should come there which is missing after "websitename.com:443 "
Can you please tell me if i am missing anything there.
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
The above uses %h, which is the "Remote hostname or IP address" for the real remote (in this case the loadbalancer).
So you need to change %h to %a which is the "Client IP address" supplied by the remoteip module after the RemoteIPHeader directive has been parsed.
LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%a %l %u %t \"%r\" %>s %b" common
More details about this behaviour and %a can be found in the docs:
I used that as well as below is my configuration right now.
LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%a %l %u %t \"%r\" %>s %b" common
CustomLog "logs/sslaccesslog" combined
Still, i am getting below error log.
192.168.255.57 - - [09/Oct/2017:10:00:32 +0000] "POST /wp-admin/admin-ajax.php HTTP/1.1" 200 47 "
Here IP "192.168.255.57" is load balancer private IP
Check with Linode that their nodebalancer uses X-Forwarded-For, its possible that they use a different header.
Change the RemoteIPInternalProxy setting to something different, like 192.168.255.0/24
If you use HTTPS, then make sure it terminates at the nodebalancer. Otherwise the nodebalancer won't be able to set the X-Forwarded-For header (because the packets are encrypted).
user -> https -> nodebalancer (can't decrypt) -> https -> web server (logs the nodebalancer ip)
user -> https -> nodebalancer (decrypt, add header) -> http -> web server (logs the user ip)
You need to switch from TCP to HTTP/HTTPS on the nodebalancer and setup your encryption keys there.
What kind of error did you receive when you tried HTTP forwarding?
I am getting redirect loop issue as i am running WordPress and this is setup with https.
1) keep nodebalancer TCP forwarding and terminate HTTPS on the webserver, but you loose the user's IP
2) change to terminating HTTPS at the nodebalancer with HTTP forwarding, and log the user's IP correctly
But with the second option, you need to modify your wordpress to accept plain HTTP connections, with special care for the wordpress template to use HTTPS links in the generated html content.
In my case, I set the following in my wp-config.php:
define('WP_HOME', 'https://…');
define('WP_SITEURL', 'https://…');
define('WPCONTENTURL', 'https://…');
I also changed the template links, so everything is https and I made sure that my .htaccess didn't redirect plain http connections.
Maybe I made more changes, I don't remember, it has been many years since I did that.
Can you please give me the configuration setting for Nodebalancer so i can check with mine as wll as on which port i will need to add nodes.
Take a look at this guide, maybe its helpful to you:
Otherwise contact linode's support via a ticket.
In my case, I set the following in my wp-config.php:
define('WP_HOME', 'https://…');
define('WP_SITEURL', 'https://…');
define('WPCONTENTURL', 'https://..
If i will do that, in that case, i will not get the redirect loop issue.
Please correct me if I am wrong.
To setup my haproxy server, I used this project