介绍:防盗链的做用是,咱们网站的图片,只能经过咱们本身的网站去访问,其余网站借用不行。我举的例子,意思是咱们的网站,被用户上传了不少图片,而用户又在他本身的网站上加上了咱们网站图片的连接,就直接能访问了。 这样能够节省他网站的带宽。 什么是referer
referer是http数据包的header的一部分,当浏览器其向服务器发送请求时,将带上referer,以此来告诉浏览器该请求时从什么网页连接过来的,浏览器处理该连接并显示。php
设置了防盗链后,一些站点将没法经过引用的方式来获取服务器的资源,只能经过直接访问本机来获取,这样就将有效的流量访问限定到了本机,不被不法站点所利用。html
怎么配置
首先编辑虚拟主机配置文件apache
#进入虚拟主机文件配置 [root@centos001 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf #添加以下 <Directory /data/wwroot/111.com> SetEnvIfNoCase Referer "http://111.com" local_ref//设为白名单 SetEnvIfNoCase Referer "http://aaa.com" local_ref//设为白名单 # SetEnvIfNoCase Referer "^$" local_ref//将空referer设为白名单,先注释掉 <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">//对txt、doc等格式的文件执行访问控制 Order Allow,Deny//执行顺序依次为allow、deny,反过来将致使都被禁止访问 Allow from env=local_ref </filesmatch> </Directory>
检查读写并重启服务vim
[root@centos001 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@centos001 ~]# /usr/local/apache2.4/bin/apachectl graceful
访问空referer(直接访问) 注释掉空referer行
centos
#配置文件中加入了第三方网站的地址 SetEnvIfNoCase Referer "http://ask.apelearn.com" local_ref
获得结果浏览器
[root@centos001 ~]# curl -e "http://111.com/qq.png" -x 192.168.10.120:80 111.com/qq.png -I HTTP/1.1 200 OK Date: Wed, 03 Jan 2018 16:44:36 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Last-Modified: Wed, 27 Dec 2017 16:41:20 GMT ETag: "e01-5615511a9ac00" Accept-Ranges: bytes Content-Length: 3585 Content-Type: image/png #这里qq出现了403,是由于使用非容许的referer会返回403状态码 [root@centos001 ~]# curl -e "http://www.qq.com/qq.png" -x 192.168.10.120:80 111.com/qq.png -I HTTP/1.1 403 Forbidden Date: Wed, 03 Jan 2018 16:47:01 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1
限制用户访问部分目录,容许特定ip访问 修改配置文件 ,检查读写后重启服务服务器
<Directory /data/wwroot/111.com/admin/> #决定规则的前后顺序 Order deny,allow Deny from all #只容许特定的ip访问 Allow from 192.168.10.120 </Directory>
测试1用指定ip访问curl
[root@centos001 admin]# curl -x 192.168.10.120:80 http://111.com/admin/index.php -I HTTP/1.1 200 OK Date: Wed, 03 Jan 2018 17:19:26 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 X-Powered-By: PHP/7.1.6 Content-Type: text/html; charset=UTF-8
换一个ip访问测试
[root@centos001 admin]# curl -x 127.0.0.1:80 http://111.com/admin/index.php -I HTTP/1.1 403 Forbidden Date: Wed, 03 Jan 2018 17:20:56 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1
除了可以限制访问目录,还能够限制某些文件的访问网站
<Directory /data/wwwroot/www.123.com> <FilesMatch "admin.php(.*)"> Order deny,allow Deny from all Allow from 127.0.0.1 </FilesMatch> </Directory>
-经过其它ip访问
[root@centos001 admin]# curl -x 127.0.0.1:80 http://111.com/admin/admin.php?123-I HTTP/1.1 403 Forbidden Date: Wed, 03 Jan 2018 17:36:42 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1
[root@centos001 admin]# curl -x 192.168.10.120:80 http://111.com/admin/admin.php?abc -I HTTP/1.1 404 Not Found Date: Wed, 03 Jan 2018 17:37:20 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1
几种限制ip的方法
http://ask.apelearn.com/question/6519
apache 自定义header
http://ask.apelearn.com/question/830
apache的keepalive和keepalivetimeout
http://ask.apelearn.com/question/556