如何在APACHE2服务器上禁止指定IP的访问


以前网站在虚拟共享主机的时候,当很多搜索引擎爬虫,特别是像360这种霸道的网络爬虫疯狂的抓取我的网站, CPU用量就会很大,接近100%,这样网站提供商就不得不禁止我的网站.

以前Fasthosts 提供了每个月免费的将我的网站提交给各大搜索引擎.昨天就发来邮件说已经提交.

monthly-traffic-search-engine-report  如何在APACHE2服务器上禁止指定IP的访问 互联网 折腾 网站信息与统计

每个月提交网站到各大搜索引擎

之后, VPS服务器 就变得非常慢,网页打开得很慢,我登陆SSH并用 htop 命令查看CPU和内存用量.

htop-100-cpu-usage  如何在APACHE2服务器上禁止指定IP的访问 互联网 折腾 网站信息与统计

htop 命令查看内存用CPU用量.

并且,我运行了以下命令查看内存用量最大的前15个进程:

1
ps augx | awk '{ print $2, $4, $11 }' | sort -k2rn | head -n 15
ps augx | awk '{ print $2, $4, $11 }' | sort -k2rn | head -n 15
view-top-memory-process  如何在APACHE2服务器上禁止指定IP的访问 互联网 折腾 网站信息与统计

查看内存使用最大的进程

然后我问了 QuickHostUK 建议,他们回得很快(半夜的时候)

Hi Zhihua,

This is not a managed VPS so we cannot give too much info without access but the Load Average is rather high at 34.08

Looking at the processes in your screen shot it seems to all be Apache processes. I would recommend checking the Apache logs for what site is being access and consider blocking the IP(s) in your firewall if they are causing too much load. 

Also you have 2 cores for CPU. If the sites you host are busy sites I would also recommend 4 cores. 

Hope this helps. 

Kind Regards,

Technical Support

QuickHostUK Limited

然后我就查看访问记录 (e.g. /var/log/apache2/access.log) 我可以从中看到有一些IP在短时间内大量的频率的访问,而且来意不善.我知道需要把它们屏蔽掉.

可以在 robots.txt 文件(例子)里屏蔽掉 (但这个并不是实时就可以更新的).

你也可以在 .htaccess 文件里将有问题的爬虫禁掉(每个网站都得设置).或者, 也可以在 php 代码的最开始 (比如 WP 目录下的 index.php) 判断访问者的 HTTP_USER_AGENT.

1
2
3
4
5
6
7
8
9
10
11
12
// helloacm.com
  $agent = '';
  if (isset($_SERVER['HTTP_USER_AGENT'])) {
      $agent = $_SERVER['HTTP_USER_AGENT'];
  }
  
  // bad bots
  define('BADBOTS','/(yisouspider|easouspider|yisou|youdaobot|yodao|360|linkscrawler|soguo)/i');
  
  if (preg_match(BADBOTS, $agent)) {
    die(); 
  }
// helloacm.com
  $agent = '';
  if (isset($_SERVER['HTTP_USER_AGENT'])) {
      $agent = $_SERVER['HTTP_USER_AGENT'];
  }
  
  // bad bots
  define('BADBOTS','/(yisouspider|easouspider|yisou|youdaobot|yodao|360|linkscrawler|soguo)/i');
  
  if (preg_match(BADBOTS, $agent)) {
    die(); 
  }

当然,如果想通过IP来判断,可以在 APACHE2 服务器的配置文件里 (e.g. /etc/apache2/apache2.conf) 将其屏蔽,将以下内容加入到该文件的最后.

<Location />
<Limit GET POST PUT>
order allow,deny
allow from all
deny from 72.220.127.178
deny from 141.101.98.148
</Limit>
</Location>

这两个IP地址我是从访问记录里过滤而来的(/var/log/apache2/access.log).在修改文件之后需要重启服务器 (命令 sudo service apache2 restart 或者 sudo service httpd restart)

你可以一次性屏蔽掉一个IP段(范围) 例如,1.2.3 实际上是 1.2.3.*

禁掉之后,在 error.log (/var/log/apache2/error.log) 里,则有可能会显示这样的访问记录:

[Wed Nov 12 16:09:32.852405 2014] [access_compat:error] [pid 3794] [client 72.220.127.178:37215] AH01797: client denied by server configuration: /var/www/helloacm.com/htdocs/favicon.ico

英文同步: https://helloacm.com/how-to-ban-specified-ips-in-apache2-server/

GD Star Rating
loading...
本文一共 516 个汉字, 你数一下对不对.
如何在APACHE2服务器上禁止指定IP的访问. (AMP 移动加速版本)
上一篇: 在LINUX下循环备份的方法
下一篇: 如何在 Linux 下 列出最耗资源的进程 (BASH 脚本)

扫描二维码,分享本文到微信朋友圈
924ffd44525421233608bc99bd3d25a1  如何在APACHE2服务器上禁止指定IP的访问 互联网 折腾 网站信息与统计

4 条评论

  1. 左尔德

评论