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

Apache使用者認證,域名跳轉,訪問日誌

Apache使用者認證

當設定了使用者認證後,使用者訪問網站時,需要輸入使用者名稱和密碼才能訪問。
可以全域性設定,也可以為某幾個虛擬主機單獨配置。
下面以全域性配置進行操作示例。

  • 編輯httpd.conf進行配置
[[email protected] ~]# vim /usr/local/apache2.4/conf/httpd.conf
  • 找到"<Directory",並新增修改相應的配置內容如下
<Directory "/usr/local/apache2.4/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    # AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    # Require all granted

    ALLOWOVERRIDE AuthConfig # 這裡相當於打開了認證開關
    AuthType Basic # 認證型別,一般使用Basic
    AuthName "test" # 自定義認證的名字,作用不大
    AuthUserFile /data/.webpasswd # 認證祕鑰檔案(使用apche自帶的工具生成)
    require valid-user # 指定需要認證的使用者為全部使用者
</Directory>
  • 建立認證檔案,新增認證使用者
[[email protected] ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.webpasswd test  # -c 建立金鑰檔案,-m 使用md5加密
New password:
Re-type new password:
Adding password for user test

# 已有檔案,新增使用者
[[email protected] ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.webpasswd test1
New password:
Re-type new password:
Adding password for user test1
  • 校驗配置修改正確性
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
  • 重新載入配置
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful
  • 測試
[[email protected] ~]# curl -x127.0.0.1:80 www.123.com  # 返回401錯誤碼
<!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>

[
[email protected]
~]# curl -x127.0.0.1:80 -utest:test www.123.com # 帶使用者名稱密碼,訪問OK <html><body><h1>It works!</h1></body></html>

域名跳轉

  • vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/tmp/web-default" # 網站資源目錄
    ServerName test.com  # 域名
    ServerAlias www.test.com www.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規則,當滿足上面的條件時,這條規則才會執行,301是永久重定向,302是臨時重定向,臨時的不會增加搜尋引擎的權重,一般都是用301
    </IfModule>
</VirtualHost>  
  • 需要檢查rewrite模組是否開啟
[[email protected] apache2.4]# /usr/local/apache2.4/bin/apachectl -M | grep rewrite
 rewrite_module (shared)
# 刪除httpd.conf 裡 rewrite_module (shared) 前面的#
# 重新載入配置
[[email protected] apache2.4]# /usr/local/apache2.4/bin/apachectl graceful
  • 測試
# curl -x127.0.0.1:80 -I www.123.com
HTTP/1.1 301 Moved Permanently
Date: Fri, 16 Nov 2018 08:10:20 GMT
Server: Apache/2.4.37 (Unix) PHP/5.6.32
Location: http://www.123.com/
Content-Type: text/html; charset=iso-8859-1

Apache訪問日誌

  • 訪問日誌記錄使用者的每一個請求
  • 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
h來源ip、l使用者密碼、u使用者、t時間、r行為,網址、s狀態碼、b大小
{Referer}瀏覽器進入一個網站後的第二個頁面,referer記錄的日誌的就是第一個訪問頁面的網址是什麼、在百度中搜索進入開源中國網站首頁後,referer記錄的就是百度搜出來的結果頁面網址
{User-Agent}使用者代理(怎麼獲得網址內容,是瀏覽器還是curl)