Nginx使用CloudFlare CDN加速后获取真实客户端IP

7天成为Magento系统架构师,现在开始学习Magento全栈开发!

《Magento2.X企业级开发实战》

Nginx使用CloudFlare CDN加速后,如果不做处理,通过 $SERVER[“REMOTEADDR”> 获取到的IP会是 CloudFlare的服务器IP Nginx 需要启用http_realip_module目录,
编译的时候带上 –with-http_realip_module
Nginx 配置加上

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 104.16.0.0/12;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 199.27.128.0/21;
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;
set_real_ip_from 2c0f:f248::/32;
set_real_ip_from 2a06:98c0::/29;

# use any of the following two
#real_ip_header CF-Connecting-IP;
real_ip_header X-Forwarded-For;
real_ip_recursive on;

如果 nginx 前面还有一层varnish的话 还要加上varnish的IP

set_real_ip_from   127.0.0.1/32;#与nginx同服务器的情况
set_real_ip_from   具体varnish服务器的ip;#与nginx不同服务器的情况

php通过 $SERVER[“REMOTEADDR”> 就能获取到真实的用户IP

相关文档 :https://support.cloudflare.com/hc/en-us/articles/200170706-How-do-I-restore-original-visitor-IP-with-Nginx-

文章来源于互联网:Nginx使用CloudFlare CDN加速后获取真实客户端IP

如无特殊说明或标注,任何个人或组织,复制、转载、采集本站内容请注明:
本文来源于:【Magento中文网】,并添加本文地址链接。
如未按上述操作复制或转载,本站有权追究法律责任。
若本站内容侵犯了原著者的合法权益,可联系我们进行处理。