1. 程式人生 > >httpd用戶認證,單個文件的用戶認證,域名跳轉,記錄訪問日誌

httpd用戶認證,單個文件的用戶認證,域名跳轉,記錄訪問日誌

httpd 認證 httpd對文件認證 httpd域名跳轉 httpd記錄訪問日誌

針對httpd用戶加驗證

修改虛擬主機配置文件。

vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那個虛擬主機編輯成如下內容
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
<Directory /data/wwwroot/www.123.com> //指定認證的目錄
AllowOverride AuthConfig //這個相當於打開認證的開關
AuthName "123.com user auth" //自定義認證的名字,作用不大

AuthType Basic //認證的類型,一般為Basic,其他類型阿銘沒用過
AuthUserFile /data/.htpasswd //指定密碼文件所在位置
require valid-user //指定需要認證的用戶為全部可用用戶
</Directory>
</VirtualHost>

****添加aming用戶並配置密碼,這個地方是難點,****
[root@aminglinux-001 ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd aming
New password:
Re-type new password:
Adding password for user aming

[root@aminglinux-001 ~]# cat /data/.htpasswd
aming:$apr1$ddMCbmzv$ZgObr361t5HLhMsdewYf2/
[root@aminglinux-001 ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd zhangsan
New password:
Re-type new password:
Adding password for user zhangsan
重新加載配置 -t,graceful
[root@aminglinux-001 wwwroot]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aminglinux-001 wwwroot]# /usr/local/apache2.4/bin/apachectl graceful

[root@aminglinux-001 wwwroot]# curl -x127.0.0.1:80 111.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@aminglinux-001 wwwroot]# curl -x127.0.0.1:80 111.com -I
HTTP/1.1 401 Unauthorized
Date: Sun, 04 Mar 2018 09:01:00 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

在本機host文件中加入本機ip 111.com
再瀏覽器嘗試,提示輸入用戶名密碼,顯示內容
技術分享圖片

如果在命令行加入-u 參數測試,-u 後面跟用戶名:密碼,也可成功。
[root@aminglinux-001 wwwroot]# curl -x127.0.0.1:80 -uaming:1q2w3e 111.com -I
HTTP/1.1 200 OK
Date: Sun, 04 Mar 2018 09:04:54 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

針對單個文件做認證
編輯虛擬主機配置文件
[root@aminglinux-001 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
技術分享圖片
技術分享圖片
重啟服務
[root@aminglinux-001 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aminglinux-001 ~]# /usr/local/apache2.4/bin/apachectl graceful

添加123.php文件並編輯

命令行訪問1111.com主機成功,訪問111.com/123.php錯誤。
[root@aminglinux-001 ~]# curl -x192.168.67.128:80 111.com/123.php
<!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@aminglinux-001 ~]# curl -x127.0.0.1:80 -uaming:1q2w3e 111.com/123.php -I
HTTP/1.1 200 OK
Date: Sun, 04 Mar 2018 09:26:59 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
登錄 網頁驗證成功。

又嘗試了如果添加不是root用戶,也不是系統用戶,只是針對httpd的用戶,新增一個k用戶,並設置密碼,也可以訪問123.php文件。
技術分享圖片

域名跳轉***

vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

RewriteRule ^/(.)$ http://111.com/$1 [R=301,L]
$1 表示的是前面第一個小括號的內容,
如果前面有第二個小括號,後面應該加$2
比如
RewriteRule ^/(.
)(【1-9】+)$ http://111.com/$1 $2[R=301,L]

定義狀態碼, R=301 ,L代表只跳轉一次,last
R=301表示永久生效
R=302表示臨時生效,不會降低原有域名權重
技術分享圖片
重新加載
技術分享圖片
檢查是否加載rewrite模塊
技術分享圖片

檢查模塊是否加載到
vi /usr/local/apache2.4/conf/httpd.conf

技術分享圖片

重新加載,看看是否加載到
技術分享圖片

修改虛擬主機配置文件,添加域名 2111.com.cn 重啟,生效

開始測試域名跳轉
技術分享圖片

技術分享圖片

顯示301 跳轉成功

403沒授權或者用戶名密碼不對
200 用戶名密碼正確
通過網頁測試訪問 2111.com.cn/123.php,跳轉到
技術分享圖片

記錄訪問日誌

編輯配置文件
vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat
修改記錄日誌格式
技術分享圖片
重新加載配置文件 -t ,graceful
開始測試:
1、查看日誌文件,沒有跳轉過來的連接跟之前相同。
2、在51博客中創建一個博客新建一個鏈接,在博客中打開鏈接後,在日誌裏即可看到新的日誌文件
技術分享圖片

httpd用戶認證,單個文件的用戶認證,域名跳轉,記錄訪問日誌