43.配置防盜鏈 訪問控制Directory及FilesMatch
11.25 配置防盜鏈
11.26 訪問控制Directory(針對一個目錄做限制)
11.27 訪問控制FilesMatch(針對一個檔案或連結做限制)
擴充套件
幾種限制ip的方法 http://ask.apelearn.com/question/6519
apache 自定義header http://ask.apelearn.com/question/830
apache的keepalive和keepalivetimeout http://ask.apelearn.com/question/556
11.25 配置防盜鏈
設定防盜鏈,不是我認識的就不允許你訪問。比如,A的圖片上傳到B,在B裡面去引用,造成B的頻寬升高,這是不允許的。只能在A域名裡去訪問,這樣圖片就能控制在我們自己的伺服器上,第三方的站點沒有辦法去引用,這個就叫防盜鏈
通過限制referer來實現防盜鏈的功能
Apache的虛擬配置檔案,配置檔案增加如下內容
<Directory /data/wwwroot/111.com> 用到了Directory。定義哪個目錄,也就是本站點
SetEnvIfNoCase Referer "http://111.com" local_ref 定義referer的白名單
SetEnvIfNoCase Referer "http://aaa.com" local_ref
SetEnvIfNoCase Referer "^$" local_ref 空的referer也設為白名單。
註釋掉的話,直接訪問網址不顯示(Forbidden),也就是沒有索引頁不能直接訪問
<FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)"> FM不嚴格區分大小寫,都可以。定義規則,針對這樣的去做白名單,也就是做防盜鏈
Order Allow,Deny 順序,先允許還是先拒絕
Allow from env=local_ref 把上面的兩個把名單做一個允許,其他的deny掉
</FilesMatch>
</Directory>
-t gaceful
curl -e "http://www.aminglinux.com/123.html" 自定義referer
例項:
[root@localhost ~]# 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
<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">
Order Allow,Deny
Allow from env=local_ref
</filesmatch>
</Directory>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName www.111.com
ServerAlias 111.com
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^111.com$
RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
</IfModule>
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "now plus 2 hour"
ExpiresByType application/x-javascript "now plus 2 hours"
ExpiresByType application/javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
</IfModule>
<Directory /data/wwwroot/111.com>
SetEnvIfNoCase Referer "http://111.com" local_ref
SetEnvIfNoCase Referer "http://aaa.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>
</VirtualHost>
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
httpd not running, trying to start
瀏覽器上測試 111.com/abc.jpg 為Forbidden(即空referer) 也就是我們需要把連結的那個站址放到referer的白名單裡去或設定空referer可訪問
curl -e "http://www.aminglinux.com/123.html 報錯原因不明
----------------------------------------------------------------------------------------------------------------------------------------------------
11.26 訪問控制Directory(針對一個目錄做限制):
只允許這個使用這個ip的使用者才能訪問(為一個目錄)
核心配置檔案內容
<Directory /data/wwwroot/111.com/admin/>
Order deny,allow Order指定順序,是先deny還是allow,和下面兩行是對應的
Deny from all
Allow from 127.0.0.1
</Directory>
curl測試狀態碼為403則被限制訪問了
-t grace
也就是做了這個操作,只有在這個ip127.0.0.1的ip上訪問,才可以200
例項:1 增加以下檔案
<Directory /data/wwwroot/111.com/admin/>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Directory>
echo "admin" > /data/wwwroot/111.com/admin/index.html
> /usr/local/apache2.4/logs/111.com-access_20170719.log
[root@localhost ~]# curl -x192.168.30.133:80 111.com/admin/index.html -I
HTTP/1.1 403 Forbidden
Date: Fri, 19 Jul 2019 08:31:27 GMT
Server: Apache/2.4.39 (Unix) PHP/5.6.32
Content-Type: text/html; charset=iso-8859-1
[root@localhost ~]# curl -x127.0.0.1:80 111.com/admin/index.html -I
HTTP/1.1 200 OK
Date: Fri, 19 Jul 2019 08:34:19 GMT
Server: Apache/2.4.39 (Unix) PHP/5.6.32
Last-Modified: Fri, 19 Jul 2019 08:29:18 GMT
ETag: "6-58e0485c5b1b2"
Accept-Ranges: bytes
Content-Length: 6
Cache-Control: max-age=0
Expires: Fri, 19 Jul 2019 08:34:19 GMT
Content-Type: text/html
例項:2
[root@localhost admin]# vi 123.php
<Directory /data/wwwroot/111.com/admin/>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Directory>
[root@localhost admin]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost admin]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost admin]# curl -x192.168.30.133:80 111.com/admin/123.php -I
HTTP/1.1 403 Forbidden 顯示為200原因不明
[root@localhost admin]# curl -x127.0.0.1:80 111.com/admin/123.php -I
HTTP/1.1 200 OK
Date: Fri, 19 Jul 2019 08:09:51 GMT
Server: Apache/2.4.39 (Unix) PHP/5.6.32
X-Powered-By: PHP/5.6.32
Cache-Control: max-age=0
Expires: Fri, 19 Jul 2019 08:09:51 GMT
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------------------------------------------------------------------------------------------------
11.27 訪問控制FilesMatch(針對一個檔案或連結做限制):
核心配置檔案內容
<Directory /data/wwwroot/111.com>
<FilesMatch "admin.php(.*)"> 以上面的directory的基礎上。在下面加一行。意思為admin.php......(什麼什麼的)
Order deny,allow
Deny from all
Allow from 127.0.0.1
</FilesMatch> 接上面的FilesMatch,都是成對出現的
</Directory>
例項:
[root@localhost admin]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<FilesMatch "admin.php(.*)">
Order deny,allow
Deny from all
Allow from 127.0.0.1
</FilesMatch>
[root@localhost admin]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost admin]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost admin]# curl -x127.0.0.1:80 'http://111.com/admin/admin.phpaew?' -I 有特殊符號要用單引號
HTTP/1.1 404 Not Found
Date: Fri, 19 Jul 2019 08:53:34 GMT
Server: Apache/2.4.39 (Unix) PHP/5.6.32
Content-Type: text/html; charset=iso-8859-1
代表頁面不存在,其實是已經訪問了,只是我們沒有建立這個admin.phpaew?
[root@localhost admin]# curl -x192.168.30.133:80 'http://111.com/admin/admin.phpaew?' -I
HTTP/1.1 403 Forbidden
Date: Fri, 19 Jul 2019 08:54:16 GMT
Server: Apache/2.4.39 (Unix) PHP/5.6.32
Content-Type: text/html; charset=iso-8859-1