一配置防盗链
通过防盗链的方式, 可以设置限制第三方的站点通过引用的方式获取服务器上的图片, 数据等, 如果想要获取本站点的图片数据, 只能通过本站点访问获取, 这样也有效的减少了服务器的资源
什么是 referer?
referer 是 http 数据包的 header 的一部分, 当浏览器其向服务器发送请求时, 将带上 referer, 以此来告诉浏览器该请求时从什么网页链接过来的, 浏览器处理该链接并显示
比如: 在 A 网站的某个也页面 http://aaa.com/a.html 里面的链接去访问 B 站的某个页面 http://bbb.com/b.html, 那么 B 网站的 reffer 就是 http://aaa.com/a.html
步骤 1: 修改虚拟主机配置文件
- [root@zlinux ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
- <Directory /data/wwwroot/123test>
- # 把 linuxtest.com 设为白名单, 对应规则 Allow
- SetEnvIfNoCase Referer "http://linuxtest.com" local_ref
- # 把某个 ip 设为白名单, 对应规则 Allow
- SetEnvIfNoCase Referer "http:http://120.78.56.104/" local_ref
- # 把空 referer 设为白名单, 对应规则 Allow; 空 referer 即直接访问的地址
- SetEnvIfNoCase Referer "^$" local_ref
- # 对 txtdoc 等格式的文件执行访问控制
- <FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">
- # 白名单地址 allow, 其他 deny
- # 执行顺序依次为 allowdeny, 反过来将导致都被禁止访问
- Order Allow,Deny
- # 白名单为 local_ref 对应的地址
- Allow from env=local_ref
- </FilesMatch>
- </Directory>
- [root@zlinux ~]# /usr/local/apache2/bin/apachectl -t
- Syntax OK
- [root@zlinux ~]# /usr/local/apache2/bin/apachectl graceful
二访问控制(Directory)
限制用户访问部分目录, 允许特定 ip 访问
步骤 1: 修改虚拟主机配置文件
- [root@zlinux ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf // 添加以下内容
- <Directory /data/wwwroot/123test/admin/>
- Order deny,allow
- # 设定 Deny 和 Allow 的先后顺序
- Deny from all
- Allow from 127.0.0.1
- # 只允许本地访问
- </Directory>
- [root@zlinux ~]# /usr/local/apache2/bin/apachectl -t
- Syntax OK
- [root@zlinux ~]# /usr/local/apache2/bin/apachectl graceful
步骤 2: 测试是否生效
- [root@zlinux ~]# curl -x 127.0.0.1:80 -e "http://linuxtest.com" linuxtest.com/admin/admintest.php -I
- HTTP/1.1 200 OK
- Date: Tue, 06 Mar 2018 09:40:48 GMT
- Server: Apache/2.4.29 (Unix) PHP/5.6.30
- X-Powered-By: PHP/5.6.30
- Cache-Control: max-age=0
- Expires: Tue, 06 Mar 2018 09:40:48 GMT
- Content-Type: text/html; charset=UTF-8
- // 来源 IP:127.0.0.1 访问成功
- [root@zlinux ~]# curl -x 192.168.204.128.1:80 -e "http://linuxtest.com" linuxtest.com/admin/admintest.php -I
curl: (5) Could not resolve proxy: 192.168.204.128.1; 未知的名称或服务
- [root@zlinux ~]# curl -x 192.168.204.128:80 -e "http://linuxtest.com" linuxtest.com/admin/admintest.php -I
- HTTP/1.1 403 Forbidden
- Date: Tue, 06 Mar 2018 09:41:41 GMT
- Server: Apache/2.4.29 (Unix) PHP/5.6.30
- Content-Type: text/html; charset=iso-8859-1
- // 非来源 IP 则被阻止访问
三访问控制(FilesMatch)
限制指定文件的访问
步骤 1: 修改配置文件
- [root@zlinux ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf // 加入以下内容
- <Directory /data/wwwroot/123test>
- // 对文件 admin.php 进行限制
- <FilesMatch "admin.php(.*)">
- Order deny,allow
- Deny from all
- Allow from 127.0.0.1
- </FilesMatch>
- </Directory>
- [root@zlinux ~]# /usr/local/apache2/bin/apachectl -t
- Syntax OK
- [root@zlinux ~]# /usr/local/apache2/bin/apachectl graceful
步骤 2: 测试
- [root@zlinux ~]# curl -x 127.0.0.1:80 http://linuxtest.com/admin/admintest.php?123 -I
- HTTP/1.1 404 Not Found
- Date: Tue, 21 Nov 2017 15:12:34 GMT
- Server: Apache/2.4.28 (Unix) PHP/5.6.30
- Content-Type: text/html; charset=iso-8859-1
- // 说明: 使用允许的 ip 访问, 由于文件不存在, 返回 404 状态码
四访问控制(user_agent)
user_agent(用户代理): 是指浏览器 (搜索引擎) 的信息包括硬件平台系统软件应用软件和用户个人偏好
有时候网站受到 CC 攻击, 其原理是: 攻击者借助代理服务器 (肉机) 生成指向受害主机的合法请求, 实现 DDOS 和伪装 CC 攻击的一个特点就是其 useragent 是一致的, 所以, 可以通过限制攻击者 useragent 的方法来阻断其攻击
步骤 1: 修改配置文件
- [root@zlinux 123test]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
- <IfModule mod_rewrite.c>
- RewriteEngine on
- RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC]
- RewriteRule .* - [F]
- # NC 表示忽略大小写, OR 选项表示或者 (不加任何选项表并且) 连接下一个条件, F=forbidden 禁止
- </IfModule>
- [root@zlinux 123test]# /usr/local/apache2/bin/apachectl -t
- Syntax OK
- [root@zlinux 123test]# /usr/local/apache2/bin/apachectl graceful
步骤 2: 测试
- [root@zlinux 123test]# curl -x 192.168.204.128:80 linuxtest.com/ -I
- HTTP/1.1 403 Forbidden
- Date: Wed, 07 Mar 2018 07:01:54 GMT
- Server: Apache/2.4.29 (Unix) PHP/5.6.30
- Content-Type: text/html; charset=iso-8859-1
- [root@zlinux 123test]# curl -A "www.baidu.com" -x 192.168.204.128:80 linuxtest.com/ -I
- HTTP/1.1 403 Forbidden
- Date: Wed, 07 Mar 2018 07:02:20 GMT
- Server: Apache/2.4.29 (Unix) PHP/5.6.30
- Content-Type: text/html; charset=iso-8859-1
- //-A 指定 user_agent
- [root@zlinux 123test]# curl -A "www" -x 192.168.204.128:80 linuxtest.com/ -I
- HTTP/1.1 200 OK
- Date: Wed, 07 Mar 2018 07:02:29 GMT
- Server: Apache/2.4.29 (Unix) PHP/5.6.30
- X-Powered-By: PHP/5.6.30
- Cache-Control: max-age=0
- Expires: Wed, 07 Mar 2018 07:02:29 GMT
- Content-Type: text/html; charset=UTF-8
第一个和第二个分别匹配对应条件, 所以状态码是 403
86.Apache(httpd)配置 -- 防盗链配置和访问控制
来源: http://www.bubuko.com/infodetail-2524540.html