系統(tǒng)運維
Apache的訪問控制有兩種:一是對目錄進行限制,一是對文件進行限制。依次介紹這兩種訪問控制方式。我們的虛擬機有兩個IP:一個127.0.0.1,另一個192.168.147.132。如果我們不想讓其中一個IP比如127.0.0.1訪問我們的網(wǎng)站。(其實主要是限制別人,不是限制自己,這里只是舉例子)
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:域名申請、網(wǎng)絡空間、營銷軟件、網(wǎng)站建設、浠水網(wǎng)站維護、網(wǎng)站推廣。編輯虛擬主機配置文件
[root@centos6 ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
添加限制127.0.0.1訪問網(wǎng)站根目錄的訪問控制方法:
……
ServerName www.test.com
ServerAlias www.aaa.com
ServerAlias www.bbb.com
AllowOverride None
Options None
Order allow,deny
Allow from all
Deny from 127.0.0.1
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.aaa.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.bbb.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
……
按Order順序匹配,與下面Allow行和Deny行的先后無關(guān)。這里Order順序為先看allow,再看deny,
所以先允許所有的IP訪問,再禁止127.0.0.1的訪問,最終結(jié)果是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
我們網(wǎng)站后臺肯定不能對任意IP開放訪問,比如只能允許在本機登錄后臺,則需要對后臺管理admin.php做白名單:正常情況下,所有人都能看到這個頁面,這樣不合適
在虛擬主機配置文件中加入如下內(nèi)容:只允許127.0.0.1訪問admin.php
……
AllowOverride None
Options None
Order allow,deny
Allow from all
Deny from 127.0.0.1
Order deny,allow
Deny from all
Allow from 127.0.0.1
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.aaa.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.bbb.com$
RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
……
檢查無誤后重新加載配置文件,可見現(xiàn)在只允許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