一、配置防盗链
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<Directory /data/wwwroot/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)">
Order Allow,Deny //其他的Deny
Allow from env=local_ref //111和aaa.com两个白名单的做允许。
</FilesMatch>
</Directory>
# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
# /usr/local/apache2.4/bin/apachectl graceful
# curl -e "http://111.com/" -x 127.0.0.1:80 111.com/lb.jpg -I
-e:指定它的referer。一定要以http://开头。
Allow:允许。
Deny:拒绝。
二、访问控制Directory
针对目录。
为了避免冲突,将这段放在防盗链的上面:
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<Directory /data/wwwroot/111.com/admin>
Order Deny,Allow //定义先deny还是allow,定义的谁就先执行谁的语句。
Deny from all //先拒绝全部
Allow from 127.0.0.1 //控制对象:来源IP
</Directory>
准备:
[root@MRX 111.com]# cd admin/
[root@MRX admin]# touch index.php
[root@MRX admin]# echo "121212" > index.php
# curl -x 127.0.0.1:80 111.com/admin/index.php -I
HTTP/1.1 200 OK
-x后是目标IP,目标IP一改变,源IP也变了。
三、访问控制FilesMatch
针对文件,链接。
# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<Directory /data/wwwroot/111.com/admin>
<FilesMatch admin.php(.*)>
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</FilesMatch>
</Directory>
# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
# /usr/local/apache2.4/bin/apachectl graceful
扩展
几种限制ip的方法 http://ask.apelearn.com/question/6519
apache 自定义header http://ask.apelearn.com/question/830
apache的keepalive和keepalivetimeout http://ask.apelearn.com/question/556