反向代理Cloudflare加速网站(SNIproxy)


写在教程前:为什么要反向代理cloudflare?答:缩短路由,加快cloudflare节点到大陆用户的速度,用过cloudflare的用户应该知道,这家CDN的速度在除了大陆以外的地方访问都非常快,那么又没有什么办法使其对大陆访问良好呢?
该操作适用环境:

①不想暴露源站,但是国内搜索引擎对cf节点抓取速度慢

②没被攻击前使用自建反代加快访问,宕机后迅速切换为cf官方节点(dnspod)

③没有使用NS接入而是CNAME

④有一台对国内访问快且访问Cloudflare也很快的服务器(如 PCCW、HKBN、TW中华电信、 bbtec)

反代程序有很多种,但是在这里我选用了SNI Proxy,为什么?因为Nginx等传统反代需要自行配置https证书,且无法如果CDN设置了强制https或重写https国内,将会出现跳转错误。

开始安装SNI Proxy

基本环境安装:

yum -y install tar wget &&   yum -y groupinstall "Development tools" &&   yum -y install pcre-devel pcre

安装辅助包libev4:

wget http://dist.schmorp.de/libev/Attic/libev-4.25.tar.gz && tar zxvf libev-4.25.tar.gz && cd libev-4.25 && ./configure -prefix=/usr/local/libev4 &&  make &&  make install

安装辅助包udns:

wget http://archive.ubuntu.com/ubuntu/pool/universe/u/udns/udns_0.4.orig.tar.gz  && tar zxvf udns_0.4.orig.tar.gz  && cd udns-0.4  && ./configure  &&  make
cd .. 
mv udns-0.4 /usr/local/udns

安装sniproxy:

wget https://down.cangshui.net/-mytargz/sniproxy.tar.gz && tar -zxvf sniproxy.tar.gz && sudo chmod -R 777 sniproxy && cd sniproxy
export CFLAGS='-I/usr/local/libev4/include -I/usr/local/udns' && export LDFLAGS='-L/usr/local/libev4/lib -L/usr/local/udns' && export LD_LIBRARY_PATH=/usr/local/libev4/lib:$LD_LIBRARY_PATH
./autogen.sh
./configure -prefix=/root
make
make install

这样sniproxy就装到了/root/sbin/sniproxy,建立一个配置文件: vi /etc/sniproxy.conf 把一下内容编辑进去:

user nobody
resolver {
nameserver 1.1.1.1 #指定NDS
mode ipv4_only #只解析IPV4
}
listen 80 {
proto http
table http_hosts
access_log { 
  filename /var/log/http_access.log   
  priority notice
     }
}
table http_hosts {
(.*.|)cangshui.net$ *#只允许自己的域名,改成自己的
}
listen 443 {
proto tls
table https_hosts
access_log { 
  filename /var/log/https_access.log   
  priority notice
     }
}
table https_hosts {
(.*.|)cangshui.net$ *#只允许自己的域名,改成自己的
}

启动SNIproxy:

/root/sbin/sniproxy -c /etc/sniproxy.conf

检查是否成功启动:

ps -ef | grep sniproxy

PS。记得iptables开放443/80端口:

iptables -I INPUT -p tcp --dport 443 -j ACCEPT 
iptables -I INPUT -p tcp --dport 80 -j ACCEPT

如何测试?本地修改HOST文件,windows端打开:

C:\Windows\System32\drivers\etc\hosts

然后写上:

你的反代服务器IP 你的域名
如:
68.68.68.68 lhzj.site

测试成功的话就可以解析到这个IP上了

需要注意的是:每次重启机器之后直接启动程序会报错

error while loading shared libraries: libev.so.4: cannot open shared object file: No such file or directory

你需要先执行

export LD_LIBRARY_PATH=/usr/local/libev4/lib:$LD_LIBRARY_PATH

然后再启动软件

猜你喜欢

转载自www.cnblogs.com/L1079991001/p/10447728.html