您好,登錄后才能下訂單哦!
Apache的訪問控制有兩種:一是對目錄進行限制,一是對文件進行限制。依次介紹這兩種訪問控制方式。我們的虛擬機有兩個IP:一個127.0.0.1,另一個192.168.147.132。如果我們不想讓其中一個IP比如127.0.0.1訪問我們的網站。(其實主要是限制別人,不是限制自己,這里只是舉例子)
編輯虛擬主機配置文件
[root@centos6 ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
添加限制127.0.0.1訪問網站根目錄的訪問控制方法:
……
ServerName www.test.com
ServerAlias www.aaa.com
ServerAlias www.bbb.com
<Directory "/data/www">
AllowOverride None
Options None
Order allow,deny
Allow from all
Deny from 127.0.0.1
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.aaa.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.bbb.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
</IfModule>
……
按Order順序匹配,與下面Allow行和Deny行的先后無關。這里Order順序為先看allow,再看deny,
所以先允許所有的IP訪問,再禁止127.0.0.1的訪問,最終結果是127.0.0.1被禁止。
檢查無誤后重新加載配置文件,可以看到我們做到了拒絕127.0.0.1的訪問,192.168.147.132仍可訪問
[root@centos6 ~]# apachectl -t
Syntax OK
[root@centos6 ~]# apachectl graceful
[root@centos6 ~]# curl -x127.0.0.1:80 -I www.test.com
HTTP/1.1 403 Forbidden
Date: Sat, 14 Jan 2017 16:18:57 GMT
Server: Apache/2.2.9 (Unix) PHP/5.4.36
Content-Type: text/html; charset=iso-8859-1
[root@centos6 ~]# curl -x192.168.147.132:80 -I www.test.com
HTTP/1.1 301 Moved Permanently
Date: Sat, 14 Jan 2017 16:19:07 GMT
Server: Apache/2.2.9 (Unix) PHP/5.4.36
X-Powered-By: PHP/5.4.36
location: forum.php
Cache-Control: max-age=0
Expires: Sat, 14 Jan 2017 16:19:07 GMT
Content-Type: text/html
[root@centos6 ~]# curl -x192.168.147.132:80 -I www.test.com/forum.php
HTTP/1.1 200 OK
Date: Sat, 14 Jan 2017 16:19:26 GMT
Server: Apache/2.2.9 (Unix) PHP/5.4.36
X-Powered-By: PHP/5.4.36
Set-Cookie: sTi8_2132_saltkey=NwiTwCJX; expires=Mon, 13-Feb-2017 16:19:26 GMT; path=/; httponly
Set-Cookie: sTi8_2132_lastvisit=1484407166; expires=Mon, 13-Feb-2017 16:19:26 GMT; path=/
Set-Cookie: sTi8_2132_sid=BreFeR; expires=Sun, 15-Jan-2017 16:19:26 GMT; path=/
Set-Cookie: sTi8_2132_lastact=1484410766%09forum.php%09; expires=Sun, 15-Jan-2017 16:19:26 GMT; path=/
Set-Cookie: sTi8_2132_onlineusernum=1; expires=Sat, 14-Jan-2017 16:24:26 GMT; path=/
Set-Cookie: sTi8_2132_sid=BreFeR; expires=Sun, 15-Jan-2017 16:19:26 GMT; path=/
Cache-Control: max-age=0
Expires: Sat, 14 Jan 2017 16:19:26 GMT
Content-Type: text/html; charset=gbk
我們網站后臺肯定不能對任意IP開放訪問,比如只能允許在本機登錄后臺,則需要對后臺管理admin.php做白名單:正常情況下,所有人都能看到這個頁面,這樣不合適
在虛擬主機配置文件中加入如下內容:只允許127.0.0.1訪問admin.php
……
<Directory "/data/www">
AllowOverride None
Options None
Order allow,deny
Allow from all
Deny from 127.0.0.1
</Directory>
<filesmatch "(.*)admin(.*)">
Order deny,allow
Deny from all
Allow from 127.0.0.1
</filesmatch>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.aaa.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.bbb.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
</IfModule>
……
檢查無誤后重新加載配置文件,可見現在只允許127.0.0.1登入后臺管理,不能通過192.168.147.132訪問后臺管理了,這樣就安全了。
[root@centos6 ~]# apachectl -t
Syntax OK
[root@centos6 ~]# apachectl graceful
[root@centos6 ~]# curl -x192.168.147.132:80 -I www.test.com/admin.php
HTTP/1.1 403 Forbidden
Date: Sat, 14 Jan 2017 16:36:15 GMT
Server: Apache/2.2.9 (Unix) PHP/5.4.36
Content-Type: text/html; charset=iso-8859-1
[root@centos6 ~]# curl -x127.0.0.1:80 -I www.test.com/admin.php
HTTP/1.1 200 OK
Date: Sat, 14 Jan 2017 16:36:25 GMT
Server: Apache/2.2.9 (Unix) PHP/5.4.36
X-Powered-By: PHP/5.4.36
Set-Cookie: sTi8_2132_saltkey=zvA82A89; expires=Mon, 13-Feb-2017 16:36:25 GMT; path=/; httponly
Set-Cookie: sTi8_2132_lastvisit=1484408185; expires=Mon, 13-Feb-2017 16:36:25 GMT; path=/
Set-Cookie: sTi8_2132_sid=qe5kCO; expires=Sun, 15-Jan-2017 16:36:25 GMT; path=/
Set-Cookie: sTi8_2132_lastact=1484411785%09admin.php%09; expires=Sun, 15-Jan-2017 16:36:25 GMT; path=/
Cache-Control: max-age=0
Expires: Sat, 14 Jan 2017 16:36:25 GMT
Content-Type: text/html; charset=gbk
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。