朋友一直反馈我这个站点打开速度不行,于是想到了免费的cloudflare CDN加速,于是就把域名的DNS解析都转到cloudflare上了,
本站用的环境是Centos的系统 +nginx + php +MariaDB
系统:Centos 6.6 x64
nginx:1.6.2
php:5.6.4 (cli) (built: Jan 8 2015 23:48:24)
MariaDB:10.0.14
开始我的nginx编译参数是
configure arguments: --prefix=/usr/local/nginx \ --user=www --group=www \ --with-http_stub_status_module \ --with-http_spdy_module \ --with-http_ssl_module \ --with-ipv6 \ --with-http_gzip_static_module \ --with-http_flv_module \ --with-ld-opt=-ljemalloc
如果需要获取cdn加速后的真实访客IP需要开启nginx的realip的模块,那么简单重新编译下nginx就是了,下面是nginx的重新编译的参数
configure arguments: --prefix=/usr/local/nginx \ --user=www --group=www \ --with-http_stub_status_module \ --with-http_spdy_module \ --with-http_ssl_module \ --with-ipv6 \ --with-http_gzip_static_module \ --with-http_flv_module \ --with-ld-opt=-ljemalloc \ --with-http_realip_module
编译好了还需要修改配置文件,我的做法是在nginx.conf文件的http段加入了以下内容 注:IP来源https://www.cloudflare.com/ips
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;
然后来看看日志的变化
修改前全部是CDN的IP
修改后都是真实访客IP
最后为了防止的CloudFlare的IP有所变动,写了个脚本做个定时计划任务就可以在CloudFlare的IP发生改变后能自动修改服务器上nginx配置文件
#!/bin/bash ######################################################################### # File Name: sync_CF_ip.sh # Author: LookBack # Email: taoxiaoyuzy#vip.qq.com # Version: # Licence: GNU General Public Licence # Created Time: Tue 13 Jan 2015 03:51:39 PM CST ######################################################################### nginxConfFile="/usr/local/nginx/conf/nginx.conf" for i in 4 6;do curl -s "https://www.cloudflare.com/ips-v$i" | while read line;do if ! grep "$line" $nginxConfFile &> /dev/null; then sed -i "/real_ip_header/i \\\tset_real_ip_from\t$line;" $nginxConfFile if service nginx configtest; then service nginx reload fi fi done done
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏