需求分析:
由于种种问题,导致蜘蛛访问和抓取量大的的时候,后台数据库高负载,影响正常的用户访问和英文平台的访问!比较推荐的做法是写robot.txt文件,但seo方面又希望对蜘蛛访问不做速度和页面方面的限制,典型的僧多粥少场景,或者使用oracle的资源计划来限制数据库用户的会话连接数,但可能对正常的用户造成影响!所以想做一个相对智能的脚本对爬虫进行适当的限制,保证数据库服务器负载正常的情况下,最大限度的允许爬虫访问,当然这只是治标不治本的方法,临时解决下,正常还是要去优化数据库的SQL,或者用缓存等手段做静态化的页面,再不然就做数据库的读写分离,hiberinate框架生成的SQL不是人看的;
策略原则:
利用防火墙,临时限制蜘蛛访问,降低oracle数据库的负载量,每隔3分钟自动关闭防火墙,尽量减少对蜘蛛访问的影响
一:前端web设置任务计划和相关脚本
[root@server195 ~]# crontab -l
*/3 * * * * /usr/local/scripts/flush_iptables.sh
[root@server195 ~]# cat /usr/local/scripts/flush_iptables.sh
#!/bin/sh
/sbin/iptables -F
/sbin/iptables -X
/sbin/iptables -Z
/sbin/iptables -t nat -F
/sbin/iptables -t nat -X
/sbin/iptables -t nat -Z
防火墙脚本进行蜘蛛IP 60秒动态抽取,该脚本由oracle服务器端来调用,web服务器和oracle服务器之间需要配置ssh密钥信任
[root@server195 ~]# cat /usr/local/scripts/deny_spider.sh
#function: deny some spider to protect oracle server
#author:lw.yang
#modify_time:2011-12-20
DATE=$(date +%Y%m%d)
rm -rf /tmp/soso_spider_ip.txt /tmp/youdao_spider_ip.txt /tmp/sogou_spider_ip.txt /tmp/baidu_spider_ip.txt
service iptables stop
tail -f /data/apache_logs/access_log_$DATE |grep -i 'spider'|grep -i 'soso' |awk -F ' ' '{print
$3}' >> /tmp/soso_spider_ip.txt &
tail -f /data/apache_logs/access_log_$DATE |grep -i 'spider'|grep -i 'youdao' |awk -F ' '
'{print $3}' >> /tmp/youdao_spider_ip.txt &
tail -f /data/apache_logs/access_log_$DATE |grep -i 'spider'|grep -i 'sogou' |awk -F ' '
'{print $3}' >> /tmp/sogou_spider_ip.txt &
tail -f /data/apache_logs/access_log_$DATE |grep -i 'spider'|grep -i 'baidu' |awk -F ' '
'{print $3}' >> /tmp/baidu_spider_ip.txt &
sleep 60
killall -9 tail
for i in $(cat /tmp/soso_spider_ip.txt /tmp/youdao_spider_ip.txt /tmp/sogou_spider_ip.txt
/tmp/baidu_spider_ip.txt|uniq);
do
iptables -A INPUT -s $i/32 -p tcp --dport 80 -j DROP
done
二:数据库端的任务计划和相关脚本
[root@server199 ~]# crontab -l
*/1 * * * * /usr/local/scripts/check_load.sh
[root@server199 ~]# cat /usr/local/scripts/check_load.sh
#function: trigger deny spider scripts on web server
LOAD=$(uptime |awk -F ',' '{print $4}' |awk -F '.' '{print $1}' |cut -d ':' -f 2)
if [ -f /tmp/begin_deny_spider.txt ];then
exit
elif [ $LOAD -gt 18 ];then
date > /tmp/begin_deny_spider.txt
ssh -p 2007 192.168.1.195 /usr/local/scripts/deny_spider.sh &
elif [ $LOAD -lt 10 ];then
rm -rf /tmp/begin_deny_spider.txt
else
fi
三:效果
限制前
<a target="_blank" href="http://blog.51cto.com/attachment/201112/142430875.jpg"></a>
限制后
<a target="_blank" href="http://blog.51cto.com/attachment/201112/142538323.jpg"></a>
web端防火墙列表
<a target="_blank" href="http://blog.51cto.com/attachment/201112/142847189.jpg"></a>
四:robot.txt文件和apache配置限制方法参考
# cat robots.txt
User-agent: *
Robot-version: 2.0
Crawl-delay: 10
Request-rate: 90/1m
Disallow: /WEB-INF/
apache配置:
SetEnvIfNoCase User-Agent "^Sogou" bad_bot
SetEnvIfNoCase User-Agent "^Sosospider" bad_bot
SetEnvIfNoCase User-Agent "^qihoobot" bad_bot
SetEnvIfNoCase User-Agent "^CollapsarWEB" bad_bot
<Directory "/data/">
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from env=bad_bot
Allow from all
</Directory>
2011年12月22日更新check_load脚本,采用后台常驻进程运行,取消crontab
[root@server199 ~]# cat /usr/local/scripts/check_load.sh
#modify_time:2011-12-22
while true
do
LOAD=$(uptime |awk -F ',' '{print $4}' |awk -F '.' '{print $1}' |cut -d ':' -f 2)
if [ $LOAD -gt 8 ];then
ssh -p 2007 192.168.1.195 /usr/local/scripts/deny_spider.sh &
fi
if [ $LOAD -lt 4 ];then
ssh -p 2007 192.168.1.195 /usr/local/scripts/flush_iptables.sh &
sleep 3
本文转自斩月博客51CTO博客,原文链接http://blog.51cto.com/ylw6006/746553如需转载请自行联系原作者
ylw6006