9月13号上课笔记

配置防盗链

防盗链,就是不让别人盗用你网站上的资源,这个资源,通常指的是图片、视频、歌曲、文档等。

 

referer的概念

你通过A网站的一个页面http://a.com/a.html 里面的链接去访问B网站的一个页面http://b.com/b.html ,那么这个B网站页面的referer就是http://a.com/a.html。 也就是说,一个referer其实就是一个网址。

 

1.配置防盗链

[root@gary-tao 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

[root@gary-tao 111.com]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@gary-tao 111.com]# /usr/local/apache2.4/bin/apachectl graceful

 

参考配置文件内容如下:

<Directory /data/wwwroot/111.com>

        SetEnvIfNoCase Referer "http://111.com" local_ref

        SetEnvIfNoCase Referer "http://111.com" local_ref

        SetEnvIfNoCase Referer "^$" local_ref

        <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">

            Order Allow,Deny

            Allow from env=local_ref

        </filesmatch>

    </Directory>

 

解释说明:

首先定义允许访问链接的referer,其中^$为空referer,当直接在浏览器里输入图片地址去访问它时,它的referer就为空。然后又使用filesmatch来定义需要保护的文件类型,访问txt、doc、mp3、zip、rar、jpg、gif、png格式的文件,当访问这样的类型文件时就会被限制。

 

修改后示例如下图:

linux配置防盗链及访问控制介绍

 

 

2.测试网页访问

浏览器访问:http://111.com/qq.png

在其它网站上链接这个网址,还是打不开。

blob.png

 

然后在虚拟主机配置文件里把第三方站点加入到白名单

[root@gary-tao 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

 

在下面图片上把第三方站点网址加入到白名单,然后保存退出重新加载配置。

linux配置防盗链及访问控制介绍

 

在点击链接http://111.com/qq.png 访问就可以了,这就是referer,如下图

blob.png

 

3.使用curl测试

[root@gary-tao 111.com]# curl -x127.0.0.1:80 111.com/qq.png -I

HTTP/1.1 200 OK

Date: Thu, 21 Dec 2017 13:41:38 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Last-Modified: Thu, 21 Dec 2017 06:18:31 GMT

ETag: "91ab-560d3ab3fbbc0"

Accept-Ranges: bytes

Content-Length: 37291

Cache-Control: max-age=86400

Expires: Fri, 22 Dec 2017 13:41:38 GMT

Content-Type: image/png

 

[root@gary-tao 111.com]# curl -e "http://wwww.qq.com/123.txt" -x127.0.0.1:80 111.com/qq.png -I  //使用-e来定义referer,这个referer一定要以http://开头,否则不管用。

HTTP/1.1 403 Forbidden

Date: Thu, 21 Dec 2017 13:42:17 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Content-Type: text/html; charset=iso-8859-1

 

[root@gary-tao 111.com]# curl -e "http://111.com/123.txt" -x127.0.0.1:80 111.com/qq.png -I

HTTP/1.1 200 OK

Date: Thu, 21 Dec 2017 13:42:34 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Last-Modified: Thu, 21 Dec 2017 06:18:31 GMT

ETag: "91ab-560d3ab3fbbc0"

Accept-Ranges: bytes

Content-Length: 37291

Cache-Control: max-age=86400

Expires: Fri, 22 Dec 2017 13:42:34 GMT

Content-Type: image/png

 

 

访问控制Directory

对于一些比较重要的网站内容,除了可以使用用户认证限制访问之外,还可以通过其他一些方法做到限制,比如可以限制IP,也可以限制user_agent,限制IP指的是限制访问网站的来源IP,而限制user_agent,通常用来限制恶意或者不正常的请求。

 

1.修改虚拟主机配置:

[root@gary-tao 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

 

核心配置文件内容如下:

  <Directory /data/wwwroot/www.123.com/admin/>

        Order deny,allow

        Deny from all

        Allow from 127.0.0.1

    </Directory>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值