Front-end Nginx, backend Apache get user real IP address
Recently in the Apache-2.2 and Apache-2.4 added to the LNMP one-click installation package, Nginx as the front end, Apache as the back end of the case, Apache can only get to the Nginx front-end IP address (127.0.0.1), and unable to obtain the user's real IP address, in this case, the backend is Apache how to obtain the user real IP address?
The Nginx configuration is as follows:
Location/{
Try_files $uri @apache;
}
Location @apache {
Internal
Proxy_pass http://127.0.0.1:8080;
Include proxy.conf;
}
Location ~. *. (PHP|PHP5)? $ {
Proxy_pass http://127.0.0.1:8080;
Include proxy.conf;
}
Proxy_connect_timeout 300s;
Proxy_send_timeout 900;
Proxy_read_timeout 900;
Proxy_buffer_size 32k;
Proxy_buffers 4 64k;
Proxy_busy_buffers_size 128k;
Proxy_redirect off;
Proxy_hide_header Vary;
Proxy_set_header accept-encoding ';
Proxy_set_header Referer $http _referer;
Proxy_set_header Cookie $http _cookie;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Get real IP address there are 2 modules of Apache:
mod_rpaf:apache-2.2 support; Apache-2.4 not supported. Many online tutorials
mod_remoteip:apache-2.4 self-brought module; Apache-2.2 support; recommended
Apache-2.2.25
MOD_RPAF Module
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
TAR-XZVF mod_rpaf-0.6.tar.gz
CD mod_rpaf-0.6/
/usr/local/apache/bin/apxs-i-c-n Mod_rpaf-2.0.slo mod_rpaf-2.0.c
Add Apache Configuration
Vi/usr/local/apache/conf/httpd.conf
Include conf/extra/httpd-rpaf.conf
Vi/usr/local/apache/conf/extra/httpd-rpaf.conf
LoadModule Rpaf_module modules/mod_rpaf-2.0.so
Rpafenable on
Rpafsethostname on
Rpafproxy_ips 127.0.0.1 10.8.0.110 # Proxy Server IP address (remember to make corresponding changes)
Rpafheader x-forwarded-for
Note: Add the IP address of the proxy server after rpafproxy_ips, there are several fill in several
Test
/usr/local/apache/bin/apachectl-t/usr/local/apache/bin/apachectl Restart look at the log
Mod_remoteip
Apache-2.2 under Configuration Mod_remoteip as follows:
Installation
wget https://github.com/ttkzw/mod_remoteip-httpd22/raw/master/mod_remoteip.c
/usr/local/apache/bin/apxs-i-c-n mod_remoteip.so mod_remoteip.c
To modify a configuration file:
Vi/usr/local/apache/conf/httpd.conf
Include conf/extra/httpd-remoteip.conf
Vi/usr/local/apache/conf/extra/httpd-remoteip.conf
LoadModule Remoteip_module modules/mod_remoteip.so
Remoteipheader x-forwarded-for
Remoteipinternalproxy 127.0.0.1
Test:
/usr/local/apache/bin/apachectl-t/usr/local/apache/bin/apachectl Restart look at the log
Apache-2.4 Configuration Mod_remoteip In addition to the above (with the MOD_REMOTEIP module does not need to install), but also need to modify the log format (toss for a long time)
Logformat "%h%a%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "" combined
Logformat "%h%a%l%u%t \"%r\ "%>s%b" common
Logformat "%h%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "%I%O" Combinedi
Add%a to the log format
Front-end Nginx, backend Apache get user real IP address