LAMP架构(5) Apache用户认证,域名跳转,Apache访问日志

Apache用户认证
此处的用户认证不是常见的用户登录,而是前置认证,只有输入用户名和密码才可以进一步浏览网页上的信息,有点像操作系统的登陆框,在登陆之前什么都看不到。

针对目录的用户认证:
配置/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
在最下面找到VirtualHost部分
<VirtualHost *: 80 > 80是http的服务端口,不用改
ServerAdmin webmaster @ test.com 网站管理员邮箱,设置成自己的网站
DocumentRoot " /data/wwwroot/www.test.com " 虚拟主机根目录放网站程序
ServerName test.com 网站名,域名
ServerAlias www . test.com 网站别名,域名别名,可写多个要用空格隔开
ErrorLog "logs/ test.com -error_log" 错误日志保存路径
CustomLog "logs/ test.com -access_log" common 日志格式,另一种combined 访问日志保存路径
</VirtualHost>
虚拟主机1不动,用下面的虚拟主机2做测试
<VirtualHost *:80> 80是http的服务端口,不用改
ServerAdmin webmaster@ 123.com 网站管理员邮箱,设置成自己的网站
DocumentRoot "/ data/wwwroot/123.com " 虚拟主机根目录放网站程序
ServerName 123.com 网站名,域名,只能写一个
< Directory /data/wwwroot/ 123.com > 指定认证的目录
AllowOverride AuthConfig 允许通过控制,认证配置,相当于打开认证的开关
AuthName "123.com user auth" 自定义认证说明,双引号中间写入认证说明
AuthType Basic 认证的类型,一般为Basic基础认证
AuthUserFile /data/.htpasswd 指定密码文件路径
require valid-user 指定需要认证的用户为全部可用用户
< /Directory >
ErrorLog "logs/ 123.com -error_log" 错误日志保存路径
CustomLog "logs/ 123.com -access_log" common 访问日志保存路径
</VirtualHost>
虚拟主机2
保存退出

创建密码文件:
[root@aliyun ~]# /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd 123
New password:
Re-type new password:
Adding password for user 123
-c是create创建用户,-m指定用户密码的加密方式为MD5,下次再创建用户时,不能再加-m选项,否则会覆盖现有的密码文件
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl -t 检测语法
Syntax OK
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl graceful 重新加载配置

测试方法1:浏览器直接访问

若点取消,网页返回提醒:
Unauthorized
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.

测试方法2:
curl -x127.0.0.1:80 123.com 状态码为401
curl -x127.0.0.1:80 -uaming:passwd 123.com 状态码为200
[root@aliyun ~]# curl -x127.0.0.1:80 123.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@aliyun ~]# curl -x127.0.0.1:80 123.com -I
HTTP/1.1 401 Unauthorized
Date: Wed, 30 May 2018 03:24:53 GMT
Server: Apache/2.4.33 (Unix) PHP/7.2.5
WWW-Authenticate: Basic realm="hello!"
Content-Type: text/html; charset=iso-8859-1

[root@aliyun ~]# curl -x127.0.0.1:80 -u 123 : 123456 123.com
我是来打酱油的! 直接取回数据,所以未显示状态码200
[root@aliyun ~]#

[root@aliyun ~]# curl -x127.0.0.1:80 -u 123 : 123456 123.com -I
HTTP/1.1 200 OK
Date: Wed, 30 May 2018 03:25:15 GMT
Server: Apache/2.4.33 (Unix) PHP/7.2.5
Last-Modified: Tue, 29 May 2018 09:23:58 GMT
ETag: "19-56d54cb48a9b1"
Accept-Ranges: bytes
Content-Length: 25
Content-Type: text/html
[root@aliyun ~]# curl -x127.0.0.1:80 - 123 : 123 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>
[root@aliyun ~]#

针对文件的用户认证:
配置/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
在最下面找到VirtualHost部分
虚拟主机1不动,用下面的虚拟主机2做测试
<VirtualHost *:80> 80是http的服务端口,不用改
ServerAdmin webmaster@ 123.com 网站管理员邮箱,设置成自己的网站
DocumentRoot "/ data/wwwroot/123.com " 虚拟主机根目录放网站程序
ServerName 123.com 网站名,域名,只能写一个
< FilesMatch /data/wwwroot/123.com/ test.php > 指定认证的目录
AllowOverride AuthConfig 允许通过控制,认证配置,相当于打开认证的开关
AuthName "123.com user auth" 自定义认证说明,双引号中间写入认证说明
AuthType Basic 认证的类型,一般为Basic基础认证
AuthUserFile /data/.htpasswd 指定密码文件路径
require valid-user 指定需要认证的用户为全部可用用户
< /FilesMatch >
ErrorLog "logs/ 123.com -error_log" 错误日志保存路径
CustomLog "logs/ 123.com -access_log" common 访问日志保存路径
</VirtualHost>
虚拟主机2
保存退出

测试方法1:
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@aliyun ~]# curl -x127.0.0.1:80 123.com -I
HTTP/1.1 200 OK 网页可以直接访问
Date: Wed, 30 May 2018 04:15:25 GMT
Server: Apache/2.4.33 (Unix) PHP/7.2.5
Last-Modified: Tue, 29 May 2018 09:23:58 GMT
ETag: "19-56d54cb48a9b1"
Accept-Ranges: bytes
Content-Length: 25
Content-Type: text/html
[root@aliyun ~]# curl -x127.0.0.1:80 123.com/ test.php -I
HTTP/1.1 401 Unauthorized 指定的test.php不能直接访问,需要认证
Date: Wed, 30 May 2018 04:16:07 GMT
Server: Apache/2.4.33 (Unix) PHP/7.2.5
WWW-Authenticate: Basic realm="i'm php test!"
Content-Type: text/html; charset=iso-8859-1
[root@aliyun ~]# curl -x127.0.0.1:80 -u 123:123456 123.com/ test.php -I
HTTP/1.1 200 OK 指定的test.php加上用户名和密码又能访问了
Date: Wed, 30 May 2018 04:16:47 GMT
Server: Apache/2.4.33 (Unix) PHP/7.2.5
X-Powered-By: PHP/7.2.5
Content-Type: text/html; charset=UTF-8
[root@aliyun ~]#
测试方法2:在浏览器里输入123.com/test.php

域名跳转
做域名跳转的目的:1、把网络上存在的旧域名链接的指向新域名。2、提升新域名的搜索引擎权重,即SEO(search engine optimization),降低旧域名的权重,跳转状态码301为永久跳转,302为临时跳转,其中302不会提升新域名权重,也不会降低旧域名权重,如果旧域名已经弃用,应该使用301跳转码!

需求,把test.com永久跳转到www.test.com,把 www.123.com域名跳转至123.com,配置如下:
配置 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
在最下面找到VirtualHost部分


虚拟主机1加入rewrite模块
<VirtualHost *: 80 > 80是http的服务端口,不用改
ServerAdmin webmaster @ test.com 网站管理员邮箱,设置成自己的网站
DocumentRoot " /data/wwwroot/www.test.com " 虚拟主机根目录放网站程序
ServerName test.com 网站名,域名
ServerAlias www .test.com 网站别名,域名别名,可写多个要用空格隔开
(配置跳转跟这里的别名无关,有无别名这一项都会跳转成功,由rewrite跳转会有301状态码和说明)
<IfModule mod_rewrite.c> 需要mod_rewrite模块支持
RewriteEngine on 打开rewrite功能
RewriteCond %{HTTP_HOST} !^ www.test.com $ www.test.com的域名请求跳转至此
RewriteRule ^/(.*)$ http:// www.test.com /$1 [R= 301,L ] 状态码301永久跳转,L=last,跳一次
定义跳转规则,将以^/(.*)$开头结尾的域名请求跳转, $1代表^/(.*)$
</IfModule>
ErrorLog "logs/ test.com -error_log" 错误日志保存路径
CustomLog "logs/ test.com -access_log" common 日志格式,另一种combined 访问日志保存路径
</VirtualHost>


虚拟主机2加入rewrite模块
<VirtualHost *:80> 80是http的服务端口,不用改
ServerAdmin webmaster@ 123.com 网站管理员邮箱,设置成自己的网站
DocumentRoot "/ data/wwwroot/123.com " 虚拟主机根目录放网站程序
ServerName 123.com 网站名,域名,只能写一个

这里并没定义ServerAlias,故在同时开启两个跳转功能的情况下,如果在每个虚拟主机的ServerName和ServerAlias里都匹配不到的情况下,会匹配默认虚拟主机的跳转设置

< Directory /data/wwwroot/ 123.com > 指定认证的目录
AllowOverride AuthConfig 允许通过控制,认证配置,相当于打开认证的开关
AuthName "123.com user auth" 自定义认证说明,双引号中间写入认证说明
AuthType Basic 认证的类型,一般为Basic基础认证
AuthUserFile /data/.htpasswd 指定密码文件路径
require valid-user 指定需要认证的用户为全部可用用户
< /Directory >
<IfModule mod_rewrite.c> 需要mod_rewrite模块支持
RewriteEngine on 打开rewrite功能
RewriteCond %{HTTP_HOST} !^123.com$ 123.com的域名请求跳转至此
RewriteRule ^/(.*)$ 123.com/$1 [R= 302 ,L] 状态码302临时跳转 ,L=last,仅一次
定义跳转规则,将以^/(.*)$开头结尾的域名请求跳转, $1代表^/(.*)$ ,可以不写主机头 http://www.
</IfModule>
ErrorLog "logs/ 123.com -error_log" 错误日志保存路径
CustomLog "logs/ 123.com -access_log" common 访问日志保存路径
</VirtualHost>
保存退出
查看是否加载rewrite模块:
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl -M|grep -i rewrite 查看是否加载rewrite模块
rewrite_module (shared) rewrite模块已加载
[root@aliyun ~]# vim /usr/local/apache2.4/conf/httpd.conf 若未加载rewrite模块
#LoadModule rewrite_module modules/mod_rewrite.so 请取消这行注释

验证跳转是否成功:
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@aliyun ~]# curl -x127.0.0.1:80 test.com -i
HTTP/1.1 301 Moved Permanently
Date: Wed, 30 May 2018 07:16:50 GMT
Server: Apache/2.4.33 (Unix) PHP/7.2.5
Location: http://www.test.com/
Content-Length: 228
Content-Type: text/html; charset=iso-8859-1

<!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.test.com /">here</a>.</p>
</body></html>
非任何虚拟主机设置的ServerName的域名请求都会匹配默认虚拟主机(即虚拟主机1)的跳转设置
[root@aliyun ~]# curl -x127.0.0.1:80 baidu.com -I
HTTP/1.1 301 Moved Permanently
Date: Wed, 30 May 2018 09:12:53 GMT
Server: Apache/2.4.33 (Unix) PHP/7.2.5
Location: http://www.test.com/
Content-Type: text/html; charset=iso-8859-1

另外一种情况: 默认虚拟主机的其它主机的跳转设置
因为虚拟主机2未设置ServerAlias www.123.com,则 ServerName 123.com的域名请求也都会被匹配默认虚拟主机(即虚拟主机1)的跳转设置,因为在httpd-vhosts.conf里找不到完全匹配的域名,即未匹配的域名请求都给默认虚拟主机处理。

[root@aliyun ~]# curl -x127.0.0.1:80 www.123.com -I 未设虚拟机2的ServerAlias,www.123.com
HTTP/1.1 301 Moved Permanently
Date: Wed, 30 May 2018 08:57:42 GMT
Server: Apache/2.4.33 (Unix) PHP/7.2.5
Location: http://www.test.com/ 被匹配默认虚拟主机的跳转设置,即未匹配的都给默认虚拟主机处理
Content-Type: text/html; charset=iso-8859-1

如果虚拟机2设置了ServerAlias www.123.com,则会匹配虚拟机2的跳转设置,到123.com
[root@aliyun ~]# curl -x127.0.0.1:80 www.123.com -I
HTTP/1.1 302 Found
Date: Wed, 30 May 2018 08:31:36 GMT
Server: Apache/2.4.33 (Unix) PHP/7.2.5
Location: http://123.com
Content-Length: 203
Content-Type: text/html; charset=iso-8859-1

总结: 如果有两个及以上的虚拟主机,且都设了跳转规则,则首先会匹配两个虚拟主机的ServerName,如果匹配到会显示200状态码,其次会匹配每个主机的ServerAlias,如果匹配到了会匹配对应主机的跳转设置和对应的状态码,最后,如果在每个主机的ServerName和ServerAlias都匹配不到对应的项目,则会匹配默认虚拟主机的跳转设置,并显示对应的状态码。

Apache访问日志
访问日志的作用非常大,不仅可以记录网站的访问情况,又可以在出现故障时为排错提供参考,和日常维护工作紧密相连。

配置访问日志:
查看访问日志类型
[root@aliyun ~]# vim /usr/local/apache2.4/conf/httpd.conf
搜索 LogFormat 查看日志记录的几种类型
<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
LogFormat "%h %l %u %t %D \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
这两行就是访问日志的记录格式 common combined ,combined中的 %D 是我加进去的参数,用来 记录连接请求耗费的时长
%h 访问IP
%l 远程登陆名,基本为空
%u 用户名,当使用用户认证时,这段位认证的用户名
%t 登陆时间
%D 连接请求耗费的时长
\"%r\" 请求的动作
%>s 最后的状态码 %s 请求的状态码
%b 传输的数据大小
\"%{Referer}i\" 访问来源
\"%{User-Agent}i\" 浏览器标识

<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio 这个是实时记录用户访问操作的日志,我们暂时不用
</IfModule>

配置访问日志类型:
[root@aliyun ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
虚拟主机1日志记录改为 combined
<VirtualHost *: 80 > 80是http的服务端口,不用改
ServerAdmin webmaster @ test.com 网站管理员邮箱,设置成自己的网站
DocumentRoot " /data/wwwroot/www.test.com " 虚拟主机根目录放网站程序
ServerName test.com 网站名,域名
ServerAlias www .test.com 网站别名,域名别名,可写多个要用空格隔开
(配置跳转跟这里的别名无关,有无别名这一项都会跳转成功,由rewrite跳转会有301状态码和说明)
<IfModule mod_rewrite.c> 需要mod_rewrite模块支持
RewriteEngine on 打开rewrite功能
RewriteCond %{HTTP_HOST} !^ www.test.com $ www.test.com的域名请求跳转至此
RewriteRule ^/(.*)$ http:// www.test.com /$1 [R= 301,L ] 状态码301永久跳转,L=last,跳一次
定义跳转规则,将以^/(.*)$开头结尾的域名请求跳转, $1代表^/(.*)$
</IfModule>
ErrorLog "logs/ test.com -error_log" 错误日志保存路径
CustomLog "logs/ test.com -access_log" combined 访问日志保存路径
</VirtualHost>


虚拟主机2日志记录改为 combinedio
<VirtualHost *:80> 80是http的服务端口,不用改
ServerAdmin webmaster@ 123.com 网站管理员邮箱,设置成自己的网站
DocumentRoot "/ data/wwwroot/123.com " 虚拟主机根目录放网站程序
ServerName 123.com 网站名,域名,只能写一个

这里并没定义ServerAlias,故在同时开启两个跳转功能的情况下,如果在每个虚拟主机的ServerName和ServerAlias里都匹配不到的情况下,会匹配默认虚拟主机的跳转设置

< Directory /data/wwwroot/ 123.com > 指定认证的目录
AllowOverride AuthConfig 允许通过控制,认证配置,相当于打开认证的开关
AuthName "123.com user auth" 自定义认证说明,双引号中间写入认证说明
AuthType Basic 认证的类型,一般为Basic基础认证
AuthUserFile /data/.htpasswd 指定密码文件路径
require valid-user 指定需要认证的用户为全部可用用户
< /Directory >
<IfModule mod_rewrite.c> 需要mod_rewrite模块支持
RewriteEngine on 打开rewrite功能
RewriteCond %{HTTP_HOST} !^123.com$ 123.com的域名请求跳转至此
RewriteRule ^/(.*)$ 123.com/$1 [R= 302 ,L] 状态码302临时跳转 ,L=last,仅一次
定义跳转规则,将以^/(.*)$开头结尾的域名请求跳转, $1代表^/(.*)$ ,可以不写主机头 http://www.
</IfModule>
ErrorLog "logs/ 123.com -error_log" 错误日志保存路径
CustomLog "logs/ 123.com -access_log" combinedio 访问日志保存路径
</VirtualHost>
保存退出

测试语法并重新加载配置
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@aliyun ~]#
查看日志记录
先来查看一下未更改前的访问记录
[root@aliyun ~]# tail /usr/local/apache2.4/logs/ test.com-access_log
164.215.247.134 - - [30/May/2018:20:59:59 +0800] "GET / HTTP/1.1" 301 227
189.46.130.124 - - [30/May/2018:21:00:24 +0800] "GET / HTTP/1.1" 301 227
186.226.216.5 - - [30/May/2018:21:31:35 +0800] "GET / HTTP/1.1" 301 227
190.162.169.25 - - [30/May/2018:21:38:01 +0800] "GET / HTTP/1.1" 301 227
185.194.175.253 - - [30/May/2018:21:46:16 +0800] "GET / HTTP/1.1" 301 227
197.157.25.114 - - [30/May/2018:21:46:18 +0800] "GET / HTTP/1.1" 301 227
125.109.163.44 - - [30/May/2018:22:45:41 +0800] "GET /music/%EF%BF%BD%DE%B0%EF%BF%BD.Wma HTTP/1.1" 301 261
39.82.52.63 - - [30/May/2018:22:59:54 +0800] "GET /music/%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD.mp3 HTTP/1.1" 301 309
95.213.187.190 - - [30/May/2018:23:14:01 +0800] "CONNECT check.best-proxies.ru:80 HTTP/1.1" 400 226
102.176.222.105 - - [30/May/2018:23:19:28 +0800] "GET / HTTP/1.1" 301 227
[root@aliyun ~]# tail /usr/local/apache2.4/logs/ 123.com-access_log
127.0.0.1 - - [30/May/2018:16:56:04 +0800] "HEAD HTTP://www.test.com/ HTTP/1.1" 200 -
127.0.0.1 - - [30/May/2018:16:57:14 +0800] "HEAD HTTP://www.test.com/ HTTP/1.1" 200 -
127.0.0.1 - - [30/May/2018:16:57:17 +0800] "HEAD HTTP://test.com/ HTTP/1.1" 302 -
127.0.0.1 - - [30/May/2018:16:57:36 +0800] "HEAD HTTP://www.test.com/ HTTP/1.1" 200 -
127.0.0.1 - - [30/May/2018:17:05:32 +0800] "HEAD HTTP://www.test.com/ HTTP/1.1" 200 -
127.0.0.1 - - [30/May/2018:17:06:42 +0800] "HEAD HTTP://www.test.com/ HTTP/1.1" 200 -
127.0.0.1 - - [30/May/2018:17:06:48 +0800] "HEAD HTTP://test.com/ HTTP/1.1" 302 -
127.0.0.1 - - [30/May/2018:17:12:41 +0800] "HEAD HTTP://test.com/ HTTP/1.1" 302 -
127.0.0.1 - - [30/May/2018:17:14:44 +0800] "HEAD HTTP://test.com/ HTTP/1.1" 302 -
127.0.0.1 - - [30/May/2018:17:48:20 +0800] "HEAD HTTP://test.com/ HTTP/1.1" 302 -
重新访问一下两个主机再看看访问记录:
[root@aliyun ~]# curl -x 127.0.0.1:80 123.com
我是来打酱油的!
[root@aliyun ~]# curl -x 127.0.0.1:80 www.test.com
我是来打醋的!
[root@aliyun ~]# tail -1 /usr/local/apache2.4/logs/ test.com-access_log
127.0.0.1 - - [31/May/2018:00:01:27 +0800] 2019 "GET HTTP:// 123.com / HTTP/1.1" 200 25 "-" "curl/7.29.0"
[root@aliyun ~]# tail -1 /usr/local/apache2.4/logs/ 123.com-access_log
combinedio 发现日志并没有生效

再次编辑httpd.conf配置文件
[root@aliyun ~]# vim /usr/local/apache2.4/conf/httpd.conf
# LoadModule logio_module modules/mod_logio.so 取消这一行的注释 #
[root@aliyun ~]# curl -x127.0.0.1:80 www.test.com
我是来打醋的!
[root@aliyun ~]# tail -1 /usr/local/apache2.4/logs/test.com-access_log
127.0.0.1 - - [31/May/2018:00:43:57 +0800] "GET HTTP://www.test.com/ HTTP/1.1" 200 22 "-" "curl/7.29.0" 125 257 记录正常了

猜你喜欢

转载自blog.csdn.net/langyue919/article/details/80505800
今日推荐