This article is a mirror article of machine translation, please click here to jump to the original article.

View: 15623|Reply: 0

[Web] Nginx on the front-end and Apache on the back-end obtain the user's real IP address

[Copy link]
Posted on 11/3/2014 9:46:05 AM | | |
Recently, he was in generalApache-2.2 andApache-2.4 to add to "lnmp one-click installation package",NginxAs a front-end, Apache can only obtain the IP address of the Nginx front-end (127.0.0.1), but not the real IP address of the user.
Nginx is configured as follows:
  1. location / {
  2.         try_files $uri @apache;
  3.         }

  4. location @apache {
  5.         internal;
  6.         proxy_pass http://127.0.0.1:8080;
  7.         include proxy.conf;
  8.         }

  9. location ~ .*\.(php|php5)?$  {
  10.         proxy_pass http://127.0.0.1:8080;
  11.         include proxy.conf;
  12.         }
Copy code
  1. proxy_connect_timeout 300s;
  2. proxy_send_timeout 900;
  3. proxy_read_timeout 900;
  4. proxy_buffer_size 32k;
  5. proxy_buffers 4 64k;
  6. proxy_busy_buffers_size 128k;
  7. proxy_redirect off;
  8. proxy_hide_header Vary;
  9. proxy_set_header Accept-Encoding '';
  10. proxy_set_header Referer $http_referer;
  11. proxy_set_header Cookie $http_cookie;
  12. proxy_set_header Host $host;
  13. proxy_set_header X-Real-IP $remote_addr;
  14. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Copy code
There are 2 modules for getting real IP addresses in Apache:
mod_rpaf: Apache-2.2 is supported; Apache-2.4 is not supported. There are many tutorials online
mod_remoteip: Apache-2.4 comes with modules; Apache-2.2 support;recommend
Apache-2.2.25
mod_rpaf module
  1. wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
  2. tar -xzvf mod_rpaf-0.6.tar.gz
  3. cd mod_rpaf-0.6/
  4. /usr/local/apache/bin/apxs  -i -c -n mod_rpaf-2.0.slo mod_rpaf-2.0.c
Copy code
Add Apache configuration
  1. vi  /usr/local/apache/conf/httpd.conf
  2. Include conf/extra/httpd-rpaf.conf
  3. vi /usr/local/apache/conf/extra/httpd-rpaf.conf

  4. LoadModule rpaf_module        modules/mod_rpaf-2.0.so
  5. RPAFenable On
  6. RPAFsethostname On
  7. RPAFproxy_ips 127.0.0.1 10.8.0.110 # 代理服务器的ip地址(记得做相应修改)
  8. RPAFheader X-Forwarded-For
Copy code
Note: Add the IP address of the proxy server after RPAFproxy_ips, and fill in a few
Test
  1. # /usr/local/apache/bin/apachectl -t
  2. # /usr/local/apache/bin/apachectl restart
  3. # 看日志
Copy code
mod_remoteip
The configuration mod_remoteip under Apache-2.2 is as follows:
Installation
  1. wget https://github.com/ttkzw/mod_remoteip-httpd22/raw/master/mod_remoteip.c
  2. /usr/local/apache/bin/apxs -i -c -n mod_remoteip.so mod_remoteip.c
Copy code
Modify the profile:
  1. vi /usr/local/apache/conf/httpd.conf
  2. Include conf/extra/httpd-remoteip.conf
  3. vi /usr/local/apache/conf/extra/httpd-remoteip.conf
  4. LoadModule remoteip_module modules/mod_remoteip.so
  5. RemoteIPHeader X-Forwarded-For
  6. RemoteIPInternalProxy 127.0.0.1
Copy code
Test:
  1. # /usr/local/apache/bin/apachectl -t
  2. # /usr/local/apache/bin/apachectl restart
  3. # 看日志
Copy code
Apache-2.4 configuration mod_remoteip in addition to the above (the module with its own mod_remoteip does not need to be installed), you also need to modify the log format (tossing for a long time)
  1. LogFormat "%h %a %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
  2. LogFormat "%h %a %l %u %t "%r" %>s %b" common
  3. LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedi
Copy code

Add %a to the log format





Previous:I heard that Du Niang can still play like this! You shake! Shake!
Next:nginx proxy mode, obtain the real IP address of the client
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com