配置防盗链、访问控制Directory与 访问控制FilesMatch

配置防盗链

介绍:防盗链的作用是,我们网站的图片,只能通过我们自己的网站去访问,其他网站借用不行。我举的例子,意思是我们的网站,被用户上传了很多图片,而用户又在他自己的网站上加上了我们网站图片的链接,就直接能访问了。 这样可以节省他网站的带宽。 什么是referer
referer是http数据包的header的一部分,当浏览器其向服务器发送请求时,将带上referer,以此来告诉浏览器该请求时从什么网页链接过来的,浏览器处理该链接并显示。

设置了防盗链后,一些站点将无法通过引用的方式来获取服务器的资源,只能通过直接访问本机来获取,这样就将有效的流量访问限定到了本机,不被不法站点所利用。

怎么配置
首先编辑虚拟主机配置文件

#进入虚拟主机文件配置
[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>

检查读写并重启服务

[root@centos001 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@centos001 ~]# /usr/local/apache2.4/bin/apachectl graceful

访问空referer(直接访问) 注释掉空referer行
输入图片说明

  • 不注释referer访问

输入图片说明

  • 第三方站点访问(引用),这里我们在论坛上发了一个链接帖子,通过这个链接访问我们虚拟主机上的图片
#配置文件中加入了第三方网站的地址
SetEnvIfNoCase Referer "http://ask.apelearn.com" local_ref

输入图片说明

得到结果

输入图片说明

  • 使用curl命令模拟referer:-e参数
[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

访问控制Directory

限制用户访问部分目录,允许特定ip访问 修改配置文件 ,检查读写后重启服务

<Directory /data/wwroot/111.com/admin/>
#决定规则的先后顺序
        Order deny,allow
        Deny from all
#只允许特定的ip访问
        Allow from 192.168.10.120
    </Directory>

测试1用指定ip访问

扫描二维码关注公众号,回复: 1067385 查看本文章
[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

访问控制FilesMatch

除了能够限制访问目录,还可以限制某些文件的访问

  • 修改配置文件
<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
  • 指定ip访问,404代表可以访问,但是文件不存在
[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

猜你喜欢

转载自my.oschina.net/u/3707523/blog/1601452