如何在Nginx中将Cloudflare IP作为额外的FastCGI参数存储?

我需要能够知道连接到我的服务器的Cloudflare IP。

这是我可以确定是否通过Tor进行连接。 要做到这一点,我需要发送客户端连接到的IP的工具。 这不是我的服务器的IP,也就是Cloudflare入口代理的IP,这就是为什么我要通过fastcgi传递Cloudflare IP。
https://www.torproject.org/projects/tordnsel.html.en

我有一个块,看起来像这样:

# Preserve Cloudflare IP fastcgi_param CF-Proxy-IP $remote_addr; fastcgi_param TEST "abc123"; # CloudFlare set_real_ip_from 199.27.128.0/21; 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/12; set_real_ip_from 172.64.0.0/13; set_real_ip_from 2400:cb00::/32; set_real_ip_from 2606:4700::/32; set_real_ip_from 2803:f800::/32; set_real_ip_from 2405:b500::/32; set_real_ip_from 2405:8100::/32; real_ip_header CF-Connecting-IP; 

但是它没有通过,我设置的虚拟头文件在phpinfo()中看不到。 我不知道还有什么我可以做,因为在real_ip_header通过后,所有原始的CF数据都将丢失。

当你使用nginx的真实IP模块时,nginx在IP地址replace时将实际的连接IP地址放在$realip_remote_addrvariables中。 所以你可以通过设置标题把它传递给你的应用程序:

 fastcgi_param CF-Proxy-IP $realip_remote_addr; 

这个variables需要nginx 1.9.7或更高版本。