以前网站在虚拟共享主机的时候,当很多搜索引擎爬虫,特别是像360这种霸道的网络爬虫疯狂的抓取我的网站, CPU用量就会很大,接近100%,这样网站提供商就不得不禁止我的网站.
以前Fasthosts 提供了每个月免费的将我的网站提交给各大搜索引擎.昨天就发来邮件说已经提交.
之后, VPS服务器 就变得非常慢,网页打开得很慢,我登陆SSH并用 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
然后我问了 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/
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK