Varnish,Nginx做后端(多重代理)时获取用户真实IP
不得不说我有点问题,网站一个Apache+Varnish不够,还要在Apache和Varnish之间插入个第三者——Nginx。 因为某些需要,也给该服务器上的个别网站上了CDN。 Apache做后端,前面多一个Nginx Proxy Cache+Varnish,获取用户的真实IP不难,只需给Apache上一个rpa
不得不说我有点问题,网站一个Apache+Varnish不够,还要在Apache和Varnish之间插入个第三者——Nginx。
因为某些需要,也给该服务器上的个别网站上了CDN。
Apache做后端,前面多一个Nginx Proxy Cache+Varnish,获取用户的真实IP不难,只需给Apache上一个rpaf模块就行了,然后让Varnish处理XFF的内容就好了:
if (req.restarts == 0) { if (req.http.x-forwarded-for) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip; } else { set req.http.X-Forwarded-For = client.ip; } }
无CDN的情况下访问网站,可以正确获取到用户的真实IP。
最近给网站上多了一个CDN,rpaf模块就显得无能为力了。因此,这几天一直苦恼如何让我的网站获取用户真实IP。
问了下那家CDN的客服,给了我这个回答:
当使用CDN后,网站服务器访问日志中的IP地址都将记录为CDN服务器的官方IP。您可以通过下列方式获取访问用户的原始IP:
ASP
Request.ServerVariables("HTTP_X_FORWARDED_FOR")
PHP
$_SERVER["HTTP_X_FORWARDED_FOR"]
JSP
request.getHeader("HTTP_X_FORWARDED_FOR")
似乎是要我修改我的网站的代码吧,不过这指标不治本,况且Ioncube加密后的,修改是没什么可能的了,问他们能否修改Nginx配置文件实现,不理我了……
看来还是得靠自己。他们既然能把那些代码发来,就证明他们的cdn服务器把真实IP存放在了HTTP_X_FORWARD_FOR里面,那么让nginx处理一下CDN服务器发来的xff的内容就行了了。
首先,取消Varnish处理XFF,把CDN传输的XFF直接交给Nginx处理,也就是注释掉以下内容:
# if (req.restarts == 0) { # if (req.http.x-forwarded-for) { # set req.http.X-Forwarded-For = # req.http.X-Forwarded-For + ", " + client.ip; # } else { # set req.http.X-Forwarded-For = client.ip; # } # }
然后在nginx的对应域名的setver层的location /层加进一行代码:
proxy_set_header ? ?X-Forwarded-For $http_x_forwarded_for;
重启Nginx,打开探针看了看,终于正常了……
原文地址:Varnish,Nginx做后端(多重代理)时获取用户真实IP, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.
