# Cloudflare 如何让 Nginx 获得真实 IP 地址

**URL:** <https://www.isharkfly.com/t/cloudflare-nginx-ip/17277>\
**Category:** DevOps\
**Tags:** nginx\
**Created:** [May 4, 2025, 5:06pm UTC](https://www.isharkfly.com/t/cloudflare-nginx-ip/17277 "2025-05-04T17:06:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hex](https://www.isharkfly.com/user_avatar/www.isharkfly.com/hex/32/21953_2.png) [@hex](https://www.isharkfly.com/u/hex)\
**Post date:** [May 4, 2025, 5:06pm UTC](https://www.isharkfly.com/t/cloudflare-nginx-ip/17277/1 "2025-05-04T17:06:04Z")

</div>

在 Nginx 的配置文件中，添加下面的内容：

```auto
		set_real_ip_from 173.245.48.0/20;
		set_real_ip_from 103.21.244.0/22;
		set_real_ip_from 103.22.200.0/22;
		set_real_ip_from 103.31.4.0/22;
		set_real_ip_from 141.101.64.0/18;
		set_real_ip_from 108.162.192.0/18;
		set_real_ip_from 190.93.240.0/20;
		set_real_ip_from 188.114.96.0/20;
		set_real_ip_from 197.234.240.0/22;
		set_real_ip_from 198.41.128.0/17;
		set_real_ip_from 162.158.0.0/15;
		set_real_ip_from 104.16.0.0/13;
		set_real_ip_from 104.24.0.0/14;
		set_real_ip_from 172.64.0.0/13;
		set_real_ip_from 131.0.72.0/22;
                 real_ip_header X-Forwarded-For;

```

上面的 IP 地址可以从 Cloud Flare 的官方网站上查询到。

查询地址：[https://www.cloudflare.com/ips-v4/#computer-program:php](https://www.cloudflare.com/ips-v4/#computer-program:php)

 ![2025-05-04_13-03-34](https://com-isharkfly-discourse.s3.dualstack.us-east-2.amazonaws.com/com-isharkfly-www/discourse-uploads/original/3X/5/f/5f0877246bf83471051128415a4a9b75c4c00387.jpeg)

配置文件，如下所示。

 ![2025-05-04_13-04-00](https://com-isharkfly-discourse.s3.dualstack.us-east-2.amazonaws.com/com-isharkfly-www/discourse-uploads/original/3X/9/e/9eb880f3c0d17c518ccd7bc1cd3546b77e974d57.jpeg)

最后从 IP 地址上，能看到已经显示为访问源的 IP 地址。

 ![2025-05-04_13-05-07](https://com-isharkfly-discourse.s3.dualstack.us-east-2.amazonaws.com/com-isharkfly-www/discourse-uploads/original/3X/5/a/5a53aef644ae09b2584b9a1b7c87aaab6b6568b0.png)
