1. 程式人生 > >Apache優化——訪問控制

Apache優化——訪問控制

Apache優化——訪問控制

11.25 配置防盜鏈

編輯虛擬主機配置文件:

[root@centos-01inux ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.example.com
<Directory /data/wwwroot/111.com>
SetEnvIfNoCase Referer "http://111.com" local_ref
SetEnvIfNoCase Referer "http://ask.apelearn.com" local_ref

SetEnvIfNoCase Referer "^$" local_ref
#定義referer白名單
<FilesMatch ".(txt|doc|mp3|zip|rar|jpg|gif|png)">
Order Allow,Deny
Allow from env=local_ref
#定義規則:允許變量local_ref指定的referer訪問,拒絕其他所有訪問。
</FilesMatch>
</Directory>

ErrorLog "logs/111.com-error_log"
SetEnvIf Request_URI ".*\.gif$" img
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
#CustomLog "logs/111.com-access_log" combined env=!img

</VirtualHost>

檢測語法錯誤並重載:
[root@centos-01inux ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@centos-01inux ~]# /usr/local/apache2.4/bin/apachectl graceful
註: 如果在referer白名單中不加“^#”(空referer),直接訪問指定內容將會被拒絕。

curl命令

curl -e 指定referer

[root@centos-01inux ~]# curl -e "http://ask.apelearn.com/" -x192.168.8.131:80 111.com/baidu.png -I

11.26 訪問控制Directory

編輯虛擬主機配置文件:

在配置文件加入如下參數:
[root@centos-01inux admin]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

……
<Directory /data/wwwroot/111.com/admin/>
Order deny,allow
Deny from all
Allow from 127.0.0.1
#只允許IP--127.0.0.1訪問“/data/wwwroot/111.com/admin/”目錄中的內容
</Directory>
……

[root@centos-01inux admin]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@centos-01inux admin]# /usr/local/apache2.4/bin/apachectl graceful

測試:
[root@centos-01inux admin]# curl -x127.0.0.1:80 111.com/admin/index.php
121212

更換IP訪問:
[root@centos-01inux admin]# curl -x192.168.8.131:80 111.com/admin/index.php -I
HTTP/1.1 403 Forbidden
Date: Wed, 02 Aug 2017 08:48:49 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

#報錯(403)!!!即,只有指定IP--127.0.0.1可以訪問該目錄。

說明:本節用於設定指定IP訪問指定目錄的權限!

11.27 訪問控制FilesMatch

使用FilesMatch參數:

[root@centos-01inux admin]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
……
<Directory /data/wwwroot/111.com>
<FilesMatch admin.php(.*)>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</FilesMatch>
</Directory>
……

[root@centos-01inux admin]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@centos-01inux admin]# /usr/local/apache2.4/bin/apachectl graceful

[root@centos-01inux admin]# curl -x127.0.0.1:80 111.com/admin.php -I
HTTP/1.1 404 Not Found
Date: Wed, 02 Aug 2017 09:24:22 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

#因為訪問的文件不存在,所以報錯:404!

說明: 本節內容應用於對某些請求設定權限。

擴展:

apache幾種限制ip的方法

禁止訪問某些文件/目錄

增加Files選項來控制,比如要不允許訪問 .inc 擴展名的文件,保護php類庫:

<Files~".inc$">
Order Allow,Deny
Deny from all
</Files>
禁止訪問某些指定的目錄

可以使用<DirectoryMatch> 正則匹配:

<Directory~"^/var/www/(.+/)*[0-9]{3}">
Order Allow,Deny
Deny from all
</Directory>
也可以使用目錄全局路徑

通過文件匹配來進行禁止,比如禁止所有針對圖片的訪問:

<FilesMatch .?i:gif|jpe?g|png)$>
Order Allow,Deny
Deny from all
<FilesMatch>
針對URL相對路徑的禁止訪問

<Location /dir/>
Order Allow,Deny
Deny from all
</Location>
apache設置自定義header

在設置自定義header前,需要先檢測一下你的httpd(Apache)是否加載了mod_headers
[root@centos-01inux ~]# /usr/local/apache2/bin/apachectl -M
如果沒有加載,需要進行加載配置。

  1. 設置header

在Apache配置文件中加入下面參數:

Header add MyHeader "Hello"
apache的keepalive和keepalivetimeout

??在APACHE的httpd.conf中,KeepAlive指的是保持連接活躍,類似於Mysql的永久連接。換一句話說,如果將KeepAlive設置為On,那麽來自同一客戶端的請求就不需要再一次連接,避免每次請求都要新建一個連接而加重服務器的負擔。

??KeepAlive的連接活躍時間當然是受KeepAliveTimeOut限制的。如果第二次請求和第一次請求之間超過KeepAliveTimeOut的時間的話,第一次連接就會中斷,再新建第二個連接。

??所以,一般情況下,圖片較多的網站應該把KeepAlive設為On。但是KeepAliveTimeOut應該設置為多少秒就是一個值得討論的問題了。

??如果KeepAliveTimeOut設置的時間過短,例如設置為1秒,那麽APACHE就會頻繁的建立新連接,當然會耗費不少的資源;反過來,如果KeepAliveTimeOut設置的時間過長,例如設置為300秒,那麽APACHE中肯定有很多無用的連接會占用服務器的資源,也不是一件好事。

??所以,到底要把KeepAliveTimeOut設置為多少,要看網站的流量、服務器的配置而定。

??其實,這和MySql的機制有點相似,KeepAlive相當於mysql connect或mysql pconnect,KeepAliveTimeOut相當於wait_timeout。

Apache優化——訪問控制