【2018.05.29學習筆記】【linux基礎知識11.18-11.21】

11.18 Apache用戶認證

有這樣一種特殊的需求:須要對網站的訪問進行安全認證,纔可以訪問網站的內容,例如公司規定網站的後臺管理頁面的訪問就是須要用戶認證,那麼能夠開啓apache的用戶認證功能來實現。php

一、在 conf/extra/httpd-vhosts.conf 虛擬主機配置文件下的進行設定。html

<VirualHost *:80>
<Directory /data/wwwroot/abc.com>   //指定須要訪問認證的網站目錄
AllowOverride AuthConfig   //這個至關於打開認證的開關
AuthName "abc user auth"   //自定義認證的名字,做用不大
AuthType Basic       //認證的類型,通常爲Basic
AuthUserFile /data/.htpasswd   //指定用戶與密碼文件所在位置
require valid-user   //指定須要認證的用戶爲所有可用用戶,即.htpasswd文件裏設定的用戶。
</Directory>
</VirualHosts>

二、建立用戶與密碼文件:-c選項是建立、-m選項是使用md5加密算法,lgs是認證用戶名算法

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

三、從新加載配置,進行訪問測試:401狀態碼就是須要認證數據庫

[root@lgs-02 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@lgs-02 ~]# /usr/local/apache2.4/bin/apachectl graceful
#直接訪問報401錯誤。
[root@lgs-02 ~]# curl -x127.0.0.1:80 abc.com -I
HTTP/1.1 401 Unauthorized
Date: Wed, 30 May 2018 12:00:36 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.32
WWW-Authenticate: Basic realm="abc user auth"
Content-Type: text/html; charset=iso-8859-1

 #指定用戶與密碼,密碼輸入錯,也報401錯誤。
[root@lgs-02 ~]# curl -x127.0.0.1:80 -ulgs:123456 abc.com -I
HTTP/1.1 401 Unauthorized
Date: Wed, 30 May 2018 12:01:37 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.32
WWW-Authenticate: Basic realm="abc user auth"
Content-Type: text/html; charset=iso-8859-1

 #指定用戶與密碼,密碼正確,認證經過,就是報200碼。
[root@lgs-02 ~]# curl -x127.0.0.1:80 -ulgs:7826078 abc.com -I
HTTP/1.1 200 OK
Date: Wed, 30 May 2018 12:01:55 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.32
X-Powered-By: PHP/5.6.32
Content-Type: text/html; charset=UTF-8

瀏覽器訪問驗證:apache

輸入圖片說明 輸入圖片說明

** 也能夠對單個文件進行訪問認證:用 <FilesMatch admin.php> 進行設定 **vim

<VirtualHost *:80>
    ServerAdmin lgs@111.com
    DocumentRoot "/data/wwwroot/111.com"
    ServerName www.111.com
    ServerAlias 111.com 123.com
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
    <FilesMatch index.php>
      AllowOverride AuthConfig
      AuthName "111 user auth"
      AuthType Basic
      AuthUserFile /data/.htpasswd
      require valid-user
    </FilesMatch>
</VirtualHost>

從新加載配置,進行驗證::對網站進行訪問不認證,對訪問指定頁面纔會提示認證。瀏覽器

[root@lgs-02 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@lgs-02 ~]# /usr/local/apache2.4/bin/apachectl graceful
#直接訪問網站主頁報200碼,不用認證。
[root@lgs-02 ~]# curl -x127.0.0.1:80 111.com -I
HTTP/1.1 200 OK
Date: Wed, 30 May 2018 12:09:22 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.32
X-Powered-By: PHP/5.6.32
Content-Type: text/html; charset=UTF-8
[root@lgs-02 ~]# curl -x127.0.0.1:80 111.com
welcome to visit 111.com[root@lgs-02 ~]# 

 #直接訪問網站admin.php,報401錯誤,須要認證。
[root@lgs-02 ~]# curl -x127.0.0.1:80 111.com/admin.php -I
HTTP/1.1 401 Unauthorized
Date: Wed, 30 May 2018 12:10:44 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.32
WWW-Authenticate: Basic realm="111 user auth"
Content-Type: text/html; charset=iso-8859-1

#指定用戶與密碼,密碼正確,認證經過,就是報200碼。
[root@lgs-02 ~]# curl -x127.0.0.1:80 -ulgs:7826078 111.com/admin.php
welcome to visit the admin page[root@lgs-02 ~]#

瀏覽器驗證:安全

輸入圖片說明 輸入圖片說明

11.19/11.20 域名跳轉

有這樣一種需求:咱們訪問123.com,瀏覽器自動跳轉到111.com去。服務器

爲何有這樣的需求:公司老用戶收藏或只記得舊域名,爲了公司品牌升級公司啓用了新的域名。因此爲了老用戶還繼續可以使用舊域名訪問公司網站,就要用到域名跳轉來實現。php7

那爲何不一樣時使用新舊兩個域名,而要進行跳轉呢?是由於涉及到網站搜索SEO的關係,搜索引擎會有蜘蛛爬蟲程序到你的網站來抓取頁面,存放到搜索引擎的數據庫中,當用戶搜到該網站內容時會反饋給用戶搜索結果,進而用戶可以訪問到你的網站裏所需的內容。

想要被搜索到,就必須提升網站的權重,他是經過域名來判斷的,假如你的網站服務器有兩個域名,那麼搜索引擎就會認爲後面的新域名是個假域名,而不計算你新域名的權重,影響客戶經過搜索引擎訪問到你的新域名的網站。因此必須使用域名跳轉。

啓用域名跳轉 :是經過Apache的Rewrite模塊來實現的(httpd.conf中啓用該模塊,httpd-vhosts.conf中定義跳轉設置)

[root@lgs-02 ~]# vim /usr/local/apache2.4/conf/httpd.conf

LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php5_module        modules/libphp5.so
#LoadModule php7_module        modules/libphp7.so
<VirtualHost *:80>
    ServerAdmin lgs@111.com
    DocumentRoot "/data/wwwroot/111.com"
    ServerName www.111.com
    ServerAlias 111.com 123.com
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
   # <FilesMatch admin.php>
   #  AllowOverride AuthConfig
   #  AuthName "111 user auth"
   #  AuthType Basic
   #  AuthUserFile /data/.htpasswd
   #  require valid-user
   # </FilesMatch>
    <IfModule mod_rewrite.c>   //編譯Apache的時候,指定了mods=most,會自動加入該模塊
     RewriteEngine on   //打開域名跳轉功能
     RewriteCond %{HTTP_HOST} !^111.com$  //定義rewrite的條件
     RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]   //定義跳轉規則 跳到 http://111.com去
    </IfModule>
</VirtualHost>

從新加載配置文件:

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

驗證跳轉測試:

[root@lgs-02 ~]# curl -x127.0.0.1:80 123.com -I
HTTP/1.1 301 Moved Permanently
Date: Wed, 30 May 2018 12:39:31 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.32
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1

[root@lgs-02 ~]# curl -x127.0.0.1:80 123.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://111.com/">here</a>.</p>
</body></html>

11.21 Apache訪問日誌

Apache的訪問日誌是在 httpd-vhosts.conf配置文件下定義的,一個虛擬主機對應一個訪問日誌:CustomLog "logs/abc.com-access_log" common

<VirtualHost *:80>
    ServerAdmin lgs@abc.com
    DocumentRoot "/data/wwwroot/abc.com"
    ServerName www.abc.com
    ServerAlias abc.com aaa.com
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common
</VirtualHost>

查看某個虛擬主機的訪問日誌

[root@lgs-02 ~]# tail /usr/local/apache2.4/logs/abc.com-access_log 
127.0.0.1 - - [30/May/2018:18:53:25 +0800] "GET HTTP://123456789.com/ HTTP/1.1" 200 24
127.0.0.1 - - [30/May/2018:19:59:42 +0800] "HEAD HTTP://abc.com/ HTTP/1.1" 200 -
127.0.0.1 - - [30/May/2018:20:00:36 +0800] "HEAD HTTP://abc.com/ HTTP/1.1" 401 -
127.0.0.1 - lgs [30/May/2018:20:01:37 +0800] "HEAD HTTP://abc.com/ HTTP/1.1" 401 -
127.0.0.1 - lgs [30/May/2018:20:01:55 +0800] "HEAD HTTP://abc.com/ HTTP/1.1" 200 -
192.168.87.1 - - [30/May/2018:20:02:28 +0800] "GET / HTTP/1.1" 401 381
192.168.87.1 - lgs [30/May/2018:20:02:47 +0800] "GET / HTTP/1.1" 200 24
127.0.0.1 - - [30/May/2018:20:38:32 +0800] "HEAD HTTP://21111.com/ HTTP/1.1" 401 -
127.0.0.1 - - [30/May/2018:20:44:45 +0800] "GET HTTP://abc.com/ HTTP/1.1" 401 381
127.0.0.1 - - [30/May/2018:20:45:05 +0800] "GET HTTP://www.abc.com/ HTTP/1.1" 401 381

訪問日誌的格式定義在: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>

通常是使用 common的簡單格式,可是這種日誌太簡單了,不能看到更詳細的信息。

咱們能夠啓動combined格式:

包含Referer信息:訪問頁面的上一級連接

User-Agent信息:用戶代理,用戶訪問頁面使用的工具:瀏覽器、curl等。

<VirtualHost *:80>
    ServerAdmin lgs@abc.com
    DocumentRoot "/data/wwwroot/abc.com"
    ServerName www.abc.com
    ServerAlias abc.com aaa.com
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" combined
[root@lgs-02 ~]# curl -x127.0.0.1:80 -ulgs:7826078 abc.com
welcome to visit abc.com[root@lgs-02 ~]# curl -x127.0.0.1:80 -ulgs:7826078 abc.com
welcome to visit abc.com[root@lgs-02 ~]# curl -x127.0.0.1:80 -ulgs:7826078 abc.com
welcome to visit abc.com[root@lgs-02 ~]#

再用ie瀏覽器訪問abc.com

輸入圖片說明

#查看日誌
[root@lgs-02 ~]# curl -x127.0.0.1:80 -ulgs:7826078 abc.com
welcome to visit tail /usr/local/apache2.4/logs/abc.com-access_log 
127.0.0.1 - - [30/May/2018:20:44:45 +0800] "GET HTTP://abc.com/ HTTP/1.1" 401 381
127.0.0.1 - - [30/May/2018:20:45:05 +0800] "GET HTTP://www.abc.com/ HTTP/1.1" 401 381
127.0.0.1 - - [30/May/2018:20:55:20 +0800] "GET HTTP://abc.com/ HTTP/1.1" 401 381 "-" "curl/7.29.0"
127.0.0.1 - lgs [30/May/2018:20:55:31 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 24 "-" "curl/7.29.0"
127.0.0.1 - lgs [30/May/2018:20:55:41 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 24 "-" "curl/7.29.0"
127.0.0.1 - lgs [30/May/2018:20:55:42 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 24 "-" "curl/7.29.0"
192.168.87.1 - - [30/May/2018:20:56:39 +0800] "GET / HTTP/1.1" 401 381 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"
192.168.87.1 - lgs [30/May/2018:20:56:45 +0800] "GET / HTTP/1.1" 401 381 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"
192.168.87.1 - lgs [30/May/2018:20:56:48 +0800] "GET / HTTP/1.1" 200 24 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"
192.168.87.1 - lgs [30/May/2018:20:56:48 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"
192.168.87.1 - lgs [30/May/2018:20:56:48 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"
192.168.87.1 - - [30/May/2018:21:01:06 +0800] "GET / HTTP/1.1" 401 381 "http://ask.apelearn.com/question/17845" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.90 Safari/537.36 2345Explorer/9.3.2.17331"
192.168.87.1 - lgs [30/May/2018:21:01:11 +0800] "GET / HTTP/1.1" 200 24 "http://ask.apelearn.com/question/17845" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.90 Safari/537.36 2345Explorer/9.3.2.17331"
192.168.87.1 - lgs [30/May/2018:21:01:11 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://www.abc.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.90 Safari/537.36 2345Explorer/9.3.2.17331"

能夠看到Referer信息:"http://ask.apelearn.com/question/17845"

和User-Agent信息:"curl/7.29.0"、"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"

相關文章
相關標籤/搜索