Apache 和 Nginx 图片以及资源防盗链
Apache防盗链:
在需要进行防盗链处理的目录,添加编辑 .htaccess 文件,文件内容为下:
1 2 3 4 5 6 7 8 9 10 11 | RewriteEngine on #是否为直接访问资源 RewriteCond %{HTTP_REFERER} !^$ [NC] #检测 referer 地址 RewriteCond %{HTTP_REFERER} !lastme.com [NC] RewriteCond %{HTTP_REFERER} !themetouch.com [NC] #匹配如下的资源类型,如果不符合上述规则的则重定向到 www.themetouch.com/下的 forbidden.png RewriteRule .*\.(gif|jpg|jpeg|png|bmp|swf|ico|txt|doc|mp3|zip)$ http://www.themetouch.com/forbidden.png [R,NC,L] |
同理,nginx的图片防盗链规则也差不多:只需要在 nginx 配置文件的server段加入下面内容,或者写入一个公共文件里面在需要使用这个规则的配置里面引入即可,注意:一定要和 location 段同级:
1 2 3 4 5 6 7 8 | location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico|txt|doc|mp3|zip)$ { access_log off; valid_referers none blocked *.lastme.com *.themetouch.com; if ($invalid_referer) { rewrite ^/ http://www.themetouch.com/forbidden.png; #return 403; } } |