在nginx下使用cf加速后會發現訪客ip都返回的cf的ip就是説儅你使用ip屏蔽時會發現無效

這時你需要安裝ngx_http_realip_module模塊加上--with-http_realip_module參數
首先需要進入nginx的安裝目錄
比如我使用的lnmp需進入

cd /root/lnmp1.0-full/nginx-1.2.7(版本不同注意)

使用命令nginx -V 查看已安裝參數

configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module

之前已進入目錄so直接添加就行了

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_mp4_module --with-http_flv_module --with-http_realip_module

等待完成
重新編譯nginx

make
make install

nginx -V 查看模組是否成功
修改nginx配置文件

vi /usr/local/nginx/conf/nginx.conf

在http下加入

   # 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   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;

參考https://support.cloudflare.com/hc/en-us/articles/200170706-Does-CloudFlare-have-an-IP-module-for-Nginx-

nginx -t 查看是否報錯正常是如下代碼

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

如無意外重啓nginx

/etc/init.d/nginx restart