11.18 Apache用戶認證 11.19/11.20 域名跳轉 11.21 Apache訪問日誌

apache用戶認證

  1. 目錄認證
[root@linux-129 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.con
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/abc.com"
    ServerName abc.com
    ServerAlias www.abc.com www.123.com
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.example.com
    <Directory /data/wwwroot/111.com>
        AllowOverride AuthConfig
        AuthName "111.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
    </Directory>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

建立密碼文件/data/.htpasswdd
apache自帶一個建立密碼的命令,htpasswd
-c 建立密碼文件
-m 密碼文件類別爲md5php

[root@linux-129 ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.passwd wuzhou
New password:
Re-type new password:
Adding password for user wuzhou

再次建立用戶密碼的時候不須要加 -c 了html

[root@linux-129 ~]# /usr/local/apache2.4/bin/htpasswd  -m /data/.passwd aming
New password:
Re-type new password:
Adding password for user aming

檢查配置文件linux

[root@linux-129 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OKapache

從新加載配置vim

[root@linux-129 ~]# /usr/local/apache2.4/bin/apachectl graceful瀏覽器

測試:緩存

[root@linux-129 ~]# curl -x127.0.0.1:80 111.com -I
HTTP/1.1 401 Unauthorized
Date: Sun, 08 Apr 2018 09:17:26 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="111.com user auth"
Content-Type: text/html; charset=iso-8859-1

這裏報401了,這裏用curl來測試,沒有加用戶名和密碼
-u 後面是指定用戶和密碼
-I 不顯示內容,只顯示狀態碼
-e 指定 referer -e 」http://www.123.com「 要是http://開頭
-A 指定user_agent(瀏覽器的標示)curl -A 「123123」服務器

[root@linux-129 ~]# curl -x127.0.0.1:80 111.com -uwuzhou:123123 -I HTTP/1.1 200 OK Date: Sun, 08 Apr 2018 09:19:09 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 X-Powered-By: PHP/7.1.6 Content-Type: text/html; charset=UTF-8curl

在window的host文件裏面添加 192.168.88.129 111.com www.example.com
ide


  1. 文件認證
    編輯虛擬配置文件
[root@linux-129 ~]# 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>
     <FilesMatch 111.php>
        AllowOverride AuthConfig
        AuthName "111.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
     </FilesMatch>
#    </Directory>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

檢查配置是否正確

[root@linux-129 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK

加載

[root@linux-129 ~]# /usr/local/apache2.4/bin/apachectl graceful

[root@linux-129 ~]# curl -x192.168.88.129:80 111.com/111.php -I
HTTP/1.1 401 Unauthorized
Date: Sun, 08 Apr 2018 09:41:48 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="111.com user auth"
Content-Type: text/html; charset=iso-8859-1
[root@linux-129 ~]# curl -x192.168.88.129:80 111.com/111.php -uwuzhou:123123 -I
HTTP/1.1 200 OK
Date: Sun, 08 Apr 2018 09:42:10 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8


域名調轉

需求,把123.com域名跳轉到www.123.com,配置以下:

<VirtualHost *:80>    
    DocumentRoot "/data/wwwroot/www.123.com"    
    ServerName www.123.com    
    ServerAlias 123.com    
    <IfModule mod_rewrite.c> 			//須要mod_rewrite模塊支持    
        RewriteEngine on 				 //打開rewrite功能    
        RewriteCond %{HTTP_HOST} !^www.123.com$  		//定義rewrite的條件,主機名(域名)不是www.123.com就知足條件,!是取反的意思   
        RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L] 		//定義rewrite規則,當知足上面的條件時,這條規則纔會執行,^是除去域名以外的部分,$1表示前面小括號的部分,L表示只跳轉一次。    
</IfModule>   
</VirtualHost>

•/usr/local/apache2/bin/apachectl -M|grep -i rewrite //若無該模塊,須要編輯配置文件httpd.conf,刪除 rewrite_module (shared) 前面的#
• curl -x127.0.0.1:80 -I 123.com //狀態碼爲301


實戰:
實現301跳轉,增長111.com的權重
編輯虛擬主機配置文件

[root@linux-129 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.example.com
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^111.com$
        RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
    </IfModule>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

配置是否正確
[root@linux-129 ~]# /usr/local/apache2.4/bin/apachectl -t

查看modeuls裏面有沒有rewrite模塊
[root@linux-129 ~]# /usr/local/apache2.4/bin/apachectl -M|grep rewrite
若是沒有就要去主配置裏面修改
進入主配置文件裏面,找到rewrite_module將#去掉

加載配置文件
[root@linux-129 ~]# /usr/local/apache2.4/bin/apachectl graceful

測試

[root@linux-129 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@linux-129 ~]# curl -x192.168.88.129:80 www.example.com -I
HTTP/1.1 301 Moved Permanently
Date: Mon, 09 Apr 2018 02:08:49 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1

404表示這個頁面不存在
401用戶名密碼認證錯誤提示的狀態碼
403 forbidden 禁止訪問
304 not modified 不須要去服務器從新下載這個圖片,緩存裏面已經有了


日誌訪問

訪問日誌記錄用戶的每個請求

日誌的格式分兩種:
combined和common
common爲系統默認格式
• vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common

系統自定義的日誌路徑:/usr/local/apache2.4/logs/

[root@linux-129 ~]# cat /usr/local/apache2.4/logs/
111.com-access_log  abc.com-error_log   httpd.pid
111.com-error_log   access_log
abc.com-access_log  error_log
[root@linux-129 ~]# cat /usr/local/apache2.4/logs/111.com-access_log
192.168.88.1 - - [09/Apr/2018:10:14:17 +0800] "GET / HTTP/1.1" 301 223

h:host,來源IP
l: u:用戶
t:時間
r:行爲
s:狀態碼,200,301,401,403,404,
b:大小,有的顯示,有的不顯示
referer:就是當前訪問網頁的上一個網址
user-agent:用戶代理,就是瀏覽器的標示
定義使用哪種日誌格式:
將虛擬主機配置文件中custlmlog 後面的common修改爲combined


修改日誌格式:
• 把虛擬主機配置文件改爲以下:

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName www.123.com
    ServerAlias 123.com
    CustomLog "logs/123.com-access_log"  combined
</VirtualHost>

• 從新加載配置文件 -t,graceful • curl -x127.0.0.1:80 -I 123.com • tail /usr/local/apache2.4/logs/123.com-access_log

使用curl和360瀏覽器在次訪問後,查看日誌:

[root@linux-129 ~]# cat /usr/local/apache2.4/logs/111.com-access_log
192.168.88.129 - - [09/Apr/2018:10:43:11 +0800] "HEAD HTTP://111.com/ HTTP/1.1" 200 - "-" "curl/7.29.0"
192.168.88.1 - - [09/Apr/2018:10:44:02 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media CenterPC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)"

紅色部分是瀏覽器的標示 網址訪問的都是那些元素:谷歌 ,

相關文章
相關標籤/搜索