A big river

Nginx obtains the real request IP address with CDN

When the server uses Nginx and CDNs are nested upstream, it is inevitable to encounter a problem:“ The CDN IP is recorded in the request log instead of the actual request IP ” 。 As we all know, this is not conducive to website management, or it is difficult to avoid some malicious risks.

It is not complicated to solve this problem. You only need to add the following configuration to the Http section of Nginx:

 set_real_ip_from 0.0.0.0/0; real_ip_header X-Forwarded-For;

Take the pagoda panel as an example“ Software Store>Installed>Nginx>Right Settings>Configuration Modification ”Find the following location to add the configuration:

 image.png

Other notes (can be ignored by those who are not skilled):

1、 set_real_ip_from It refers to the IP address or IP segment of the upstream proxy forwarding server. It can be understood that all the requests from these IPs are processed as real IP acquisition, similar to a white list. The 0.0.0.0/0 set in this tutorial refers to all request IPs.

2、 real_ip_header It's not difficult to understand. It means that the real IP address is obtained from the request header. In most cases, the upstream CDN will send HTTP_X_FORWARDED_FOR The request header contains the IP string of the origin. For example, "127.0.0.1127.0.0.2127.0.0.3". Generally speaking, obtaining the first non empty IP is true.

3. This tutorial takes the pagoda as an example. Nginx installed in the pagoda environment comes with ngx_http_realip_module This module supports obtaining the real IP address. If the Nginx compiled and installed by default does not exist, it needs to be installed and started by yourself before the configuration can take effect. The method of installing this module will not be repeated in this article.

(End)

Comment

◎ Express your opinions, exchange your opinions, and keep friendly remarks.

search
Recent messages
Recently published
Site Label