设置全站只能通过CDN访问

发布于 / 系统运维 / 4 条评论

为什么这么做:前段时间网站一直被慢速CC困扰,尽管开启cloudflare的五秒盾之后世界都安静了,但是这样会影响到用户体验

而且我发现经常有一些恶意IP扫描端口,并且绕过CDN进行网站访问

这个月的访问流量
Cloudflare,YES!

考虑到这个放博客的VPS并没有部署其他的服务,因此我决定直接全站只允许CDN访问(当然还包括自己家IP啦)


iptables -I INPUT -s 自己的IP -p tcp --dport 22 -j ACCEPT    #ssh默认22端口,不过我改了

iptables -I INPUT -p tcp --dport 1:65535 -j DROP       #全端口TCP关闭

#############只允许CDN(cloudflare)的IP访问###########

iptables -A INPUT -p tcp --dport 80 -s 173.245.48.0/20 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 103.21.244.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 103.22.200.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 103.31.4.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 141.101.64.0/18 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 108.162.192.0/18 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 190.93.240.0/20 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 188.114.96.0/20 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 197.234.240.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 198.41.128.0/17 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 162.158.0.0/15 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 104.16.0.0/12 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 172.64.0.0/13 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -s 131.0.72.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 173.245.48.0/20 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 103.21.244.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 103.22.200.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 103.31.4.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 141.101.64.0/18 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 108.162.192.0/18 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 190.93.240.0/20 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 188.114.96.0/20 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 197.234.240.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 198.41.128.0/17 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 162.158.0.0/15 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 104.16.0.0/12 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 172.64.0.0/13 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -s 131.0.72.0/22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -j DROP
iptables -A INPUT -p tcp --dport 443 -j DROP

在你测试这段代码没有问题能正常工作之后,可以永久保存此规则

service iptables save

执行这个命令的时候有时候可能会报错:The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

这是因为没有安装iptables服务,直接使用yum安装iptables服务即可.

yum install iptables-services

安装完成后,重新执行 service iptables save 命令即可保存成功。

firewall设置指令:

firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=103.21.244.0/22 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=103.22.200.0/22 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=103.31.4.0/22 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=104.16.0.0/12 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=108.162.192.0/18 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=131.0.72.0/22 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=162.158.0.0/15 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=172.64.0.0/13 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=173.245.48.0/20 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=188.114.96.0/20 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=190.93.240.0/20 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=197.234.240.0/22 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=198.41.128.0/17 port port=80  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=103.21.244.0/22 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=103.22.200.0/22 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=103.31.4.0/22 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=104.16.0.0/12 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=108.162.192.0/18 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=131.0.72.0/22 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=162.158.0.0/15 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=172.64.0.0/13 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=173.245.48.0/20 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=188.114.96.0/20 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=190.93.240.0/20 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=197.234.240.0/22 port port=443  protocol=tcp  accept"
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=198.41.128.0/17 port port=443  protocol=tcp  accept"

#使规则生效
firewall-cmd --reload

PS:cloudflare的IP列表查看https://www.cloudflare.com/ips/

其他CDN的IP段列表看这里:链接

  1. 云起

    大佬,这样设置了以后是不是搜索蜘蛛也访问不了了啊。是不是也得把搜做蜘蛛放入白名单啊。

  2. cat66

    想要請教右下角哪人偶代碼相關問題 請問站長能否聯繫呢,謝謝

  3. 折纸

    你底部能不能再丑点

    1. 沧水
      @折纸 xswl