1. 程式人生 > >5.Apache用戶認證,域名跳轉,訪問日誌

5.Apache用戶認證,域名跳轉,訪問日誌

用戶認證 域名跳轉 訪問日誌

[toc]

Apache用戶認證

11.18 Apache用戶認證

用戶認證功能就是在用戶訪問網站的時候,需要輸入用戶名密碼才能進行訪問。一些比較好總要的站點和網站後臺都會加上用戶認證,以保證安全。

1.下面對xavi.com站點來做一個全站的用戶認證:

vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把xavi.com那個虛擬主機編輯成如下內容
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/xavi.com"
    ServerName xavi.com
    <Directory /data/wwwroot/xavi.com> //指定認證的目錄
        AllowOverride AuthConfig //這個相當於打開認證的開關
        AuthName "xavi.com user auth" //自定義認證的名字,作用不大
        AuthType Basic //認證的類型,一般為Basic,其他類型阿銘沒用過
        AuthUserFile /data/.htpasswd  //指定密碼文件所在位置
        require valid-user //指定需要認證的用戶為全部可用用戶
    </Directory>
</VirtualHost>

2.創建密碼htpasswd命令

在創建密碼文件先要了解htpasswd命令:
htpasswd命令是Apache的Web服務器內置工具,用於創建和更新儲存用戶名、域和用戶基本認證的密碼文件。

[root@xavi ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd xavi
New password: 
Re-type new password: 
Adding password for user xavi
[root@xavi ~]# ls /data/.htpasswd
/data/.htpasswd
[root@xavi ~]# cat !$
cat /data/.htpasswd
xavi:$apr1$WKpg/kJm$gLaC.HA8/GbaF8g/fSVx/1

2.1 再創建一個用戶,重新加載配置-t,graceful

[root@xavi ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd lilei
New password: 
Re-type new password: 
Adding password for user lilei
[root@xavi ~]# cat /data/.htpasswd
xavi:$apr1$WKpg/kJm$gLaC.HA8/GbaF8g/fSVx/1
lilei:$apr1$f8p3nVfN$gP/WTgkIpWPTqoTI8V31U1
//重新加載配置-t,graceful
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful

2.2 綁定hosts,瀏覽器測試,狀態碼為401,curl -x127.0.0.1:80 xavi.com

[root@xavi ~]# curl -x127.0.0.1:80 xavi.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn‘t understand how to supply
the credentials required.</p>
</body></html>
[root@xavi ~]# curl -x127.0.0.1:80 xavi.com -I
HTTP/1.1 401 Unauthorized
Date: Tue, 06 Mar 2018 14:50:18 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="xavi.com user auth"
Content-Type: text/html; charset=iso-8859-1

技術分享圖片

技術分享圖片

技術分享圖片

3.curl -x127.0.0.1:80 -uaming:passwd www.123.com //狀態碼為200

[root@xavi ~]# curl -x127.0.0.1:80 -uxavi:xavi2018 xavi.com
xavi.com[root@xavi ~]#
[root@xavi ~]# curl -x127.0.0.1:80 -uxavi:xavi2018 xavi.com -I
HTTP/1.1 200 OK
Date: Tue, 06 Mar 2018 15:12:44 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

xavi.com[root@xavi ~]# curl -x127.0.0.1:80 -uxavi:xavi xavi.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn‘t understand how to supply
the credentials required.</p>
</body></html>

4. 單個文件進行認證

4.1 在配置文件中添加以下類似內容(根據自己的目錄修改):

[root@xavi ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

技術分享圖片

4.2 重新加載配置-t,graceful

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

4.3 編輯一個123.php文件,並認證

[root@xavi ~]# vim /data/wwwroot/xavi.com/123.php

技術分享圖片

[root@xavi ~]# curl -x127.0.0.1:80 -uxavi:xavi2018 xavi.com/123.php
123.php[root@xavi ~]# 

10.19 域名跳轉

301 域名跳轉

1 配置域名跳轉vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

域名跳轉類似於將網頁重新指向另一個網站,但區別是域名跳轉會將域名本身重新指向網站,而不使用HTML或腳本來進行重新指向。當域名被設置為跳轉至另一網站,域名的地址將不會保留在瀏覽器的URL欄中,該欄顯示的會是新頁面的URL。如果您希望保留該欄中的URL,則需要使用隱形跳轉。

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/xavi.com"
    ServerName xavitest.com
    ServerAlias www.example.com www.xavi.com
    <IfModule mod_rewrite.c>          //需要mod_rewrite模塊支持
        RewriteEngine on                   //打開rewrite功能
        RewriteCond %{HTTP_HOST} !^xavitest.com$     //定義rewrite的條件,主機名(域名)不是xavitest.com滿足條件
                RewriteRule ^/(.*)$ http://xavitest.com/$1 [R=301,L]     //定義rewrite規則:當滿足上面條件時才執行當前規則,即跳轉到xavitest.com。狀態碼301表示永久跳轉;302表示臨時跳轉。L表示last,執行一次,^表示非,(.*)表示123.php,$1表示第一個方括號
   </IfModule>

    ErrorLog "logs/xavi.example.com-error_log"
    CustomLog "logs/xavi.example.com-access_log" common
</VirtualHost>

技術分享圖片

<VirtualHost *:80>

     DocumentRoot "/data/wwwroot/xavi.com"
    ServerName xavi.com
    ServerAlias www.example.com
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^xavi.com$
        RewriteRule ^/(.*)$ http://www.xavi.com/$1 [R=301,L]
   </IfModule>

        ErrorLog "logs/xavi-error_log"
    CustomLog "logs/xavi-access_log" common
</VirtualHost>

檢查錯誤,打開httpd服務,重新加載配置-t,graceful

[root@xavi ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful
httpd not running, trying to start
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl start
httpd (pid 3152) already running
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful

2.修改httpd.conf文件

[root@xavi ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite
[root@xavi ~]# vim /usr/local/apache2.4/conf/httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so     //去掉#,以啟用這個模塊

技術分享圖片

/usr/local/apache2/bin/apachectl -M|grep -i rewrite //若無該模塊,需要編輯配置文件

[root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite
 rewrite_module (shared)
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful

3.測試跳轉是否成功

技術分享圖片

80端口有幾個冒號就是啟動了幾個網卡

[root@xavi ~]# curl -x192.168.72.130:80 xavi.com
xavi.com[root@xavi ~]# curl -x192.168.122.1:80 abcd.com
this is a test[root@xavi ~]# 

curl -x192.168.122.1:80 www.example.com -I //-I可直接查看結果

[root@xavi ~]# curl -x192.168.122.1:80 www.example.com -I
HTTP/1.1 301 Moved Permanently
Date: Wed, 07 Mar 2018 13:43:47 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Location: http://www.xavi.com/
Content-Type: text/html; charset=iso-8859-1
[root@xavi ~]# curl -x192.168.122.1:80 www.example.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.xavi.com/">here</a>.</p>
</body></html>

4.狀態碼總結 301,200,401

11.21 Apache訪問日誌

1. Apache訪問日誌所在位置:

[root@xavi ~]#  ls /usr/local/apache2.4/logs/
abcd-access_log      abcd-error_log  httpd.pid            xavi.com-error_log
abcd.com-access_log  access_log      xavi-access_log      xavi-error_log
abcd.com-error_log   error_log       xavi.com-access_log
[root@xavi ~]# ls /usr/local/apache2.4/logs/xavi.com-access_log
/usr/local/apache2.4/logs/xavi.com-access_log
[root@xavi ~]# cat !$

技術分享圖片

2. 查看日誌格式

2.1 在httpd.conf搜索LogFormat

[root@xavi ~]# vim /usr/local/apache2.4/conf/httpd.conf
<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>

combined和common兩種格式,默認使用common格式,Referer上一條訪問的網址.

3. 更改日誌的格式為combined

[root@xavi ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^xavi.com$
        RewriteRule ^/(.*)$ http://www.xavi.com/$1 [R=301,L]
   </IfModule>

        ErrorLog "logs/xavi-error_log"
    CustomLog "logs/xavi-access_log" combined
</VirtualHost>

技術分享圖片

查看日誌文件:cat /usr/local/apache2.4/logs/xavi-access_log

之前未找到原因日誌變化的原因是寫錯了訪問名

[root@xavi ~]# cat /usr/local/apache2.4/logs/xavi-access_log

5.Apache用戶認證,域名跳轉,訪問日誌