您好,登錄后才能下訂單哦!
1.在虛擬主機中實現用戶驗證
[root@localhost logs]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/www"
ServerName www.123.com
ServerAlias www.aaa.com
#用戶認證
<Directory *>
AllowOverride AuthConfig
AuthName "study"
AuthType Basic
AuthUserFile /data/.htpasswd //存用戶名和密碼
require valid-user
</Directory>
#ErrorLog "logs/dummy-host.example.com-error_log" //錯誤日志
#CustomLog "logs/dummy-host.example.com-access_log" common //正常日志
</VirtualHost>
/data/.htpasswd文件的生成:
/usr/local/apache2/bin/htpasswd -c -m(md5)/data/.htpasswd [username] //第一次使用時要添加-c選項,若第二次還是用則會覆蓋第一次的所產生的文件。
New password:
Re-typenew password:
[root@localhost mysql]# cat /data/.htpasswd
aming1:$apr1$60e7Z/11$31yiwDyX0iRSVGAuznpwn.
[root@localhost ~]# apache -t
Syntax OK
[root@localhost ~]# apache restart
注:
增加第二個用戶的時候,就不要加-c了,因為-c是創建的意思,如果加上會把這個文件重寫。
再次用瀏覽器進入論壇會出現以下窗口:
輸入用戶名和密碼后就會進入論壇了。
2.配置域名跳轉
在用戶認證后添加以下內容:
#域名跳轉
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.123.com$
RewriteRule ^/(.*)$ http://www.aaa.com/$1 [R=301,L]
</IfModule>
如果是多個域名,可以這樣設置:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.123.com [OR]
RewriteCond %{HTTP_HOST} ^www.456.com$
RewriteRule ^/(.*)$ http://www.aaa.com/$1 [R=301,L]
</IfModule>
通過輸入curl命令則可以驗證域名跳轉成功,但是如果未指定用戶名則不能跳轉
[root@localhost logs]# curl -u aming1:123456 -x127.0.0.1:80 www.123.com -I
HTTP/1.1 301 Moved Permanently
Date: Sat, 02 May 2015 22:08:07 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Location: http://www.aaa.com/
Cache-Control: max-age=0
Expires: Sat, 02 May 2015 22:08:07 GMT
Content-Type: text/html; charset=iso-8859-1
[root@localhost logs]# curl -u aming1:123456 -x127.0.0.1:80 www.aaa.com -I
HTTP/1.1 301 Moved Permanently
Date: Sat, 02 May 2015 22:08:14 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
X-Powered-By: PHP/5.3.28
location: forum.php
Cache-Control: max-age=0
Expires: Sat, 02 May 2015 22:08:14 GMT
Content-Type: text/html
以下為未輸入用戶名所得的結果:
[root@localhost mysql]# curl -x127.0.0.1:80 www.aaa.com -I
HTTP/1.1 401 Authorization Required
Date: Sat, 02 May 2015 22:02:56 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
WWW-Authenticate: Basic realm="study"
Content-Type: text/html; charset=iso-8859-1
[root@localhost mysql]# curl -x127.0.0.1:80 www.123.com -I
HTTP/1.1 301 Moved Permanently
Date: Sat, 02 May 2015 22:02:58 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Location: http://www.aaa.com/
Cache-Control: max-age=0
Expires: Sat, 02 May 2015 22:02:58 GMT
Content-Type: text/html; charset=iso-8859-1
注:301與302區別——301暫時,302永久 ,401 Authorization Requied需要認證
配置apache的訪問日志
[root@localhost logs]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
添加如下內容
#配置日志
ErrorLog "/usr/local/apache2/logs/dummy-host.example.com-error_log"
SetEnvIf Request_URI ".*\.gif$" p_w_picpath-request
SetEnvIf Request_URI ".*\.jpg$" p_w_picpath-request
SetEnvIf Request_URI ".*\.png$" p_w_picpath-request
SetEnvIf Request_URI ".*\.bmp$" p_w_picpath-request
SetEnvIf Request_URI ".*\.swf$" p_w_picpath-request
SetEnvIf Request_URI ".*\.js$" p_w_picpath-request
SetEnvIf Request_URI ".*\.css$" p_w_picpath-request
CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/oem.discuz.qq.com-error_%Y%m%d.log 86400" combined env=!p_w_picpath-request
注:路徑最好寫絕對路徑,同時也要寫對,在實驗時由于自己未寫對路徑就出現了錯誤。
若將生成日志的文件路徑寫錯會出現以下錯誤。
[root@localhost logs]# curl -u aming1:123456 -x127.0.0.1:80 www.123.com -I
curl: (7) couldn't connect to host
[root@localhost logs]# curl -u aming1:123456 -x127.0.0.1:80 www.aaa.com -I
curl: (7) couldn't connect to host
配置靜態文件緩存
[root@localhost logs]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
添加如下內容
#配置靜態文件緩存
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType p_w_picpath/gif "access plus 1 days"
ExpiresByType p_w_picpath/jpeg "access plus 24 hours"
ExpiresByType p_w_picpath/png "access plus 24 hours"
ExpiresByType text/css "now plus 2 hour"
ExpiresByType application/javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
</IfModule>
注:我們在設置js文件時格式為x-javascript,curl后不能達到我們要的效果,我們將其該為javascript就可以了。
[root@localhost www]# curl -u aming1:123456 -x127.0.0.1:80 www.aaa.com/2.js -I
HTTP/1.1 200 OK
Date: Sat, 02 May 2015 22:43:13 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Last-Modified: Sat, 02 May 2015 22:41:14 GMT
ETag: "23277-0-5152106b3c108"
Accept-Ranges: bytes
Cache-Control: max-age=0
Expires: Sat, 02 May 2015 22:43:13 GMT
Content-Type: application/javascript
[root@localhost www]# curl -u aming1:123456 -x127.0.0.1:80 www.aaa.com/2.js -I
HTTP/1.1 200 OK
Date: Sat, 02 May 2015 22:44:14 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Last-Modified: Sat, 02 May 2015 22:41:14 GMT
ETag: "23277-0-5152106b3c108"
Accept-Ranges: bytes
Cache-Control: max-age=7200
Expires: Sun, 03 May 2015 00:44:14 GMT
Content-Type: application/javascript
這里補充說明一點,最后一行已經告訴了我們文件類型,比如這里是 Content-Type: application/javascript,如果對于 css 文件,則為 Content-Type: text/css ,對于 jpg/jpeg 文件,則為 Content-Type: p_w_picpath/jpeg ,只需要將該類型配置到 ExpiresByType 中就可以對相應文件進行緩存了。
配置防盜鏈
添加如下內容:
#配置防盜鏈
SetEnvIfNoCase Referer "^http://www.123.com" local_ref
SetEnvIfNoCase Referer ".*www.aaa.com" local_ref
SetEnvIfNoCase Referer "^$" local_ref
<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">
Order Allow,Deny
Allow from env=local_ref
</filesmatch>
實驗結果:
[root@localhost www]# curl -u aming1:123456 -e"www.aaa.com/safsaf" -x127.0.0.1:80 www.aaa.com/1.txt -I
HTTP/1.1 200 OK
Date: Sat, 02 May 2015 22:59:35 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Last-Modified: Sat, 02 May 2015 15:22:31 GMT
ETag: "2325b-a-5151ae5c57002"
Accept-Ranges: bytes
Content-Length: 10
Cache-Control: max-age=0
Expires: Sat, 02 May 2015 22:59:35 GMT
Content-Type: text/plain
[root@localhost www]# curl -u aming1:123456 -e"http://www.123.com/safsaf" -x127.0.0.1:80 www.aaa.com/1.txt -I
HTTP/1.1 200 OK
Date: Sat, 02 May 2015 23:00:34 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Last-Modified: Sat, 02 May 2015 15:22:31 GMT
ETag: "2325b-a-5151ae5c57002"
Accept-Ranges: bytes
Content-Length: 10
Cache-Control: max-age=0
Expires: Sat, 02 May 2015 23:00:34 GMT
Content-Type: text/plain
[root@localhost www]# curl -e "www.baidu.com" -u aming1:123456 -x127.0.0.1:80 www.aaa.com/1.txt -I
HTTP/1.1 403 Forbidden
Date: Sat, 02 May 2015 22:57:23 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Content-Type: text/html; charset=iso-8859-1
[root@localhost www]# curl -e "www.qq.com" -u aming1:123456 -x127.0.0.1:80 www.aaa.com/1.txt -I
HTTP/1.1 403 Forbidden
Date: Sat, 02 May 2015 22:57:44 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Content-Type: text/html; charset=iso-8859-1
訪問控制
<Directory /data/www/>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Directory>
針對請求的uri去限制
<filesmatch "(.*)admin(.*)">
Order deny,allow
Deny from all
Allow from 127.0.0.1
</filesmatch>
某個某陸下禁止解析php
<Directory /data/www/path>
php_admin_flag engine off
<filesmatch "(.*)php">
Order deny,allow
Deny from all
</filesmatch>
</Directory>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。