Apache用戶認證,域名跳轉

11月15日任務php

11.18 Apache用戶認證html

11.19/11.20 域名跳轉linux

11.21 Apache訪問日誌apache

 

11.18 Apache用戶認證vim

  • 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>
  •  /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd aming 
  •  從新加載配置-t , graceful
  •  綁定hosts,瀏覽器測試
  •  curl -x127.0.0.1:80 www.123.com //狀態碼爲401
  •  curl -x127.0.0.1:80 -uaming:passwd www.123.com //狀態碼爲200

 

[root@zgxlinux-01 ~]# vim /usr/local/apache2.4.37/conf/extra/httpd-vhosts.conf 
瀏覽器

[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/htpasswd -c -m /data/.htppasswd zhangguoxiang     #-c 建立    -m 表示md5加密
curl

[root@zgxlinux-01 ~]# cat /data/.htppasswd 
zhangguoxiang:$apr1$RKbRex0f$R0QtyiT0EuEsfJmxwSwhl0
[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/htpasswd -m /data/.htppasswd zhangsan        #這時候不須要指定-c 由於這個文件已經建立過
New password: 
Re-type new password: 
Adding password for user zhangsan
[root@zgxlinux-01 ~]# cat /data/.htppasswd 
zhangguoxiang:$apr1$RKbRex0f$R0QtyiT0EuEsfJmxwSwhl0
zhangsan:$apr1$q/7YWXFI$OQJYI5OfO8in3KdDYi.Eo1
[root@zgxlinux-01 ~]#  /usr/local/apache2.4.37/bin/apachectl -t
Syntax OK
[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/apachectl graceful
[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/apachectl graceful
[root@zgxlinux-01 ~]# 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>ide

 

#針對單文件操做測試

  • 還能夠針對單個文件進行認證
  • <VirtualHost *:80>
  •     DocumentRoot "/data/wwwroot/www.123.com"
  •     ServerName www.123.com
  •     <FilesMatch admin.php>
  •         AllowOverride AuthConfig
  •         AuthName "123.com user auth"
  •         AuthType Basic
  •         AuthUserFile /data/.htpasswd
  •         require valid-user
  •     </FilesMatch>
  • </VirtualHost>

#建立一個123phoui

[root@zgxlinux-01 ~]# vim /data/wwwroot/111.com/123.php

 

11.19 、域名跳轉

  • 需求,把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規則,當知足上面的條件時,這條規則纔會執行
  • </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

#編輯配置文件 /usr/local/apache2.4.37/conf/extra/httpd-vhosts.conf   ,301表示永久跳轉

[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/apachectl -t
Syntax OK
[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/apachectl graceful

[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/apachectl -M |grep rewrite
[root@zgxlinux-01 ~]# vim /usr/local/apache2.4.37/conf/httpd.conf   #修改配置文件,註釋這一行,加載module模塊。

[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/apachectl -M |grep rewrite
 rewrite_module (shared)

[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/apachectl -t
Syntax OK
[root@zgxlinux-01 ~]# /usr/local/apache2.4.37/bin/apachectl graceful
[root@zgxlinux-01 ~]# curl -x192.168.56.128:80 2111.com.cn -I     #-I表示不顯示結果而顯示狀態碼
HTTP/1.1 301 Moved Permanently
Date: Sat, 17 Nov 2018 09:43:04 GMT
Server: Apache/2.4.37 (Unix) PHP/5.6.32
Location: http://www.111.com/
Content-Type: text/html; charset=iso-8859-1

[root@zgxlinux-01 ~]# curl -x192.168.56.128:80 2111.com.cn 
<!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.111.com/">here</a>.</p>
</body></html>

 

11.21 、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 

  •  把虛擬主機配置文件改爲以下: 

         <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_lo

 

[root@zgxlinux-01 ~]# ls /usr/local/apache2.4.37/logs/
111.com-access_log  abc.com-access_log  access_log  httpd.pid
111.com-error_log   abc.com-error_log   error_log

[root@zgxlinux-01 ~]# vim /usr/local/apache2.4.37/conf/httpd.conf

[root@zgxlinux-01 ~]# vim /usr/local/apache2.4.37/conf/extra/httpd-vhosts.conf 

 

相關文章
相關標籤/搜索