配置文件增加如下内容vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<Directory /data/wwwroot/www.123.com>
SetEnvIfNoCase Referer "http://111.com" local_ref
SetEnvIfNoCase Referer "http://www.111.com" local_ref
SetEnvIfNoCase Referer "^$" local_ref
#定义referer白名单,#如果在referer白名单中不加“^#”(空referer),直接访问指定内容将会被拒绝
<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">
Order Allow,Deny
Allow from env=local_ref
#针对"\.(txt|doc|mp3|zip|rar|jpg|gif|png)"定义规则:允许变量local_ref指定的referer访问,拒绝其他所有访问
</filesmatch>
</Directory>
[root@aminglinux-02 ~]# curl -e "http://www.baidu.com" -x127.0.0.1:80 111.com/123.png -I
HTTP/1.1 200 OK
Date: Wed, 02 Aug 2017 13:58:41 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Last-Modified: Wed, 02 Aug 2017 13:52:13 GMT
ETag: "425c-555c5922e9d40"
Accept-Ranges: bytes
Content-Length: 16988
Content-Type: image/png
核心配置文件内容vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<Directory /data/wwwroot/www.123.com/admin/>
Order deny,allow
##Order定义先deny还是先allow,两步都要执行
Deny from all
Allow from 127.0.0.1
</Directory>
[root@aminglinux-02 admin]# curl -x127.0.0.1:80 111.com/admin/index.php
dadada[root@aminglinux-02 admin]# curl -x192.168.16.120:80 111.com/admin/index.php -I
HTTP/1.1 403 Forbidden
Date: Wed, 02 Aug 2017 14:29:02 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1
<Directory /data/wwwroot/111.com>
<FilesMatch "admin.php(.*)">
Order deny,allow
Deny from all
Allow from 127.0.0.1
</FilesMatch>
</Directory>
原文地址:http://blog.51cto.com/akui2521/2122707