网站借助iptables或.htaccess阻止IP恶意攻击访问

  • A+
所属分类:Linux

背景:今天突然发现博客打开巨慢,然后就各种排查,最后发现日志里一直有某个固定IP地址一直不停的在访问,系统CPU负载飙升,借助iptables.htaccess来阻止该访问。

1. iptable方式

    这种方式可以在系统层面阻挡某些IP访问,尤其是恶意攻击情况,往往会有很多ip访问,可以通过限制某些频繁出现的IP地址来减缓服务器压力,iptables通过维护一张ip表来实现控制访问功能。

(1)封杀命令

    iptables -I INPUT -s 192.168.1.1 -j DROP

(2)解封命令

    iptables -D INPUT -s 192.168.1.1 -j DROP

说明:参数-I Insert,-D Delete,192.168.1.1为要干掉的ip地址。

(3)查看ip表

    iptables -list

    

2. .htaccess

    该文件并不一定适用所有,因为有的网站不需要这个文件,若有的话可以在里面添加命令同样来实现IP访问,在该文件下添加如下红色部分。

    # BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#</IfModule>

order allow,deny
deny from 191.96.249.57
allow from all
</IfModule>
# END WordPress

说明:.htaccess路径比较自由,一般会放在网站的根目录中。

  

圈里圈外

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: