<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>
</VirtualHost> ## httpd的用户认证 - 浏览器在打开一个网站,什么页面都不现实,只弹出一个对话框,让你输入用户名和密码,只有输入正确才能访问网站的内容 - 需求 - abc.com这个网站访问的时候,不能直接访问,必须输入用户名和密码,验证通过之后才能访问网站内容——>这样做的目的是增加安全性,但是劣势是用户体验很差,因为每个人用访问网站都必须输入用户名和密码 1. 编辑vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 文件 ``` [root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
在文件中编辑配置文件,将以下内容添加到第二段代码中——>因为第一个虚拟主机是默认虚拟主机 <Directory /data/wwwroot/www.111.com> //指定认证的目录 AllowOverride AuthConfig //这个相当于打开认证的开关,如果没有这一行,那就相当于没有开启 AuthName "111.com user auth" //自定义认证的名字,作用不大 AuthType Basic //认证的类型,一般为Basic,其他类型几乎没用过 AuthUserFile /data/.htpasswd //指定密码文件所在位置——>这里需要指定一个用户名的密码文件 require valid-user //指定需要认证的用户为全部可用用户 </Directory>
更改完的代码 <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost>
<VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com <Directory /data/wwwroot/111.com> AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </Directory> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost>
然后保存退出
2. 用apache自带的命令htpasswd创建
- /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd hanfeng
- -c参数,就是创建
-m参数,使用MD5加密
- /data/.htpasswd,指定密码文件所在位置
- useradd hanfeng,(这里的useradd 是不需要写的,直接写用户名就行)加一个用户名(密码为hanfeng)
[root@hf-01 ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd hanfeng New password: Re-type new password: Adding password for user hanfeng [root@hf-01 ~]#
- 若是提示错误,那么先查看文件是否生成,然后查看文件内容,再去排查其他错误
3. 查看 /data/.htpasswd 文件内容
- 能看到文件中有一行,以 : 冒号为分割。左边是用户名,右边是MD5加密的密码
- 因为是用 -m 指定了加密的类型
[root@hf-01 ~]# cat /data/.htpasswd hanfeng:$apr1$DAYH22/X$YbawXM95jlmckPykpfn3u/ [root@hf-01 ~]#
4. 再增加zhangsan用户,就不需要去 -c参数 创建了,因为已经创建过了(密码为123456)
[root@hf-01 ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd zhangsan New password: Re-type new password: Adding password for user zhangsan [root@hf-01 ~]#
5. 查看文件内容,会发现又增加了一行密码
[root@hf-01 ~]# cat /data/.htpasswd hanfeng:$apr1$DAYH22/X$YbawXM95jlmckPykpfn3u/ zhangsan:$apr1$NC7LZ5JQ$FBZNAIzjCTwKheTiWrtlT. [root@hf-01 ~]#
6. 查看配置文件是否有错误,并重新加载配置文件
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful [root@hf-01 ~]#
7. 测试,访问111.com的时候,会提示401状态码
- 401状态码,说明访问的内容需要做用户认证
[root@hf-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> [root@hf-01 ~]# curl -x127.0.0.1:80 111.com -I HTTP/1.1 401 Unauthorized Date: Wed, 20 Dec 2017 15:34:22 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
[root@hf-01 ~]#
8. 同样可以在浏览器中输入111.com——>前提是要先在物理机中hosts文件定义111.com
192.168.202.130 www.abc.com www.123.com 111.com
9. 会发现需要输入用户名,和密码
![输入图片说明](https://static.oschina.net/uploads/img/201712/20154417_RfRv.jpg "浏览器访问111.com,会提示输入密码")
10. 在输入用户名和密码
![输入图片说明](https://static.oschina.net/uploads/img/201712/20155044_Duav.jpg "输入验证码和密码")
11. 会发现正常访问到页面了
![输入图片说明](https://static.oschina.net/uploads/img/201712/20155247_rti9.jpg "正常访问到页面内容")
12. 这个就是用户认证
### curl输入用户名和密码
- curl -x127.0.0.1:80 -uhanfeng:hanfeng 111.com -I
- -u参数,然后加用户名,再:冒号密码
[root@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:hanfeng 111.com -I HTTP/1.1 200 OK Date: Wed, 20 Dec 2017 15:57:06 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@hf-01 ~]#
- 这时会发现状态码改变了,状态码变成了200(200即为正常)
- 若是输错密码,那么状态码又会变成401
[root@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:feng 111.com -I HTTP/1.1 401 Unauthorized Date: Wed, 20 Dec 2017 15:59:16 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
[root@hf-01 ~]#
## 针对某一个访问的进行认证
- 针对 admin.php文件 只有打开这个文件才会执行下面的操作
<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>
- 这里和前面的用户认证唯一不同的就是,使用的是 FilesMatch ,当访问的文件匹配到admin.php的时候,它才去执行以下的配置,而前面的用户认证使用的Directory,指定了一个目录,只要是这个目录下面的都会去认证,这里是FilesMatch ,匹配文件的
1. 更改配置文件,注释掉Directory,去使用FilesMatch
- vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
未更改前 <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost>
<VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com <Directory /data/wwwroot/111.com> AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </Directory> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost>
更改后 <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost>
<VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com
<FilesMatch 123.php>
AllowOverride AuthConfig
AuthName "111.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</FilesMatch>
#</Directory>
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
</VirtualHost>
并保存退出
2. 检查配置文件是否存在语法错误,并重新加载配置文件
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful [root@hf-01 ~]#
3. 编辑123.php文件
[root@hf-01 ~]# vim /data/wwwroot/111.com/123.php
在配置文件中写入
<?php echo "123.php"; 并保存退出 ``` 4. 检查是否能访问网站,这里会看到不加-u 也能访问到网站,状态码也是200,而不是401了 ``` [root@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:feng 111.com 111.com[root@hf-01 ~]# [root@hf-01 ~]# curl -x127.0.0.1:80 111.com 111.com[root@hf-01 ~]# [root@hf-01 ~]# curl -x127.0.0.1:80 111.com -I HTTP/1.1 200 OK Date: Wed, 20 Dec 2017 16:24:13 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@hf-01 ~]# ``` 5. 但是在访问123.php的时候,会提示401,这是因为针对123.php做了一个限制 ``` [root@hf-01 ~]# curl -x127.0.0.1:80 111.com/123.php -I HTTP/1.1 401 Unauthorized Date: Wed, 20 Dec 2017 16:25:42 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 [root@hf-01 ~]# ``` 6. 这时候-u指定用户名和密码,就可以访问123.php了 ``` [root@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:hanfeng 111.com/123.php -I HTTP/1.1 200 OK Date: Wed, 20 Dec 2017 16:27:11 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@hf-01 ~]# curl -x127.0.0.1:80 -uhanfeng:hanfeng 111.com/123.php 123.php[root@hf-01 ~]# ``` # 11.19/11.20 域名跳转 ## 域名跳转概要目录 - 需求,把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 ## 域名跳转 - 域名跳转,常用于 老域名跳转到新域名,因为老用户已经习惯记住一个域名了,突然页面换到了新的域名,就需要做这个操作。一个域名就是一个网站的名称,都可以通过域名访问到该网站 - 因为域名涉及到一个SEO,SEO会对网站的内容进行一个价值权重定义,旧网站价值高,会一直存在各大搜索引擎,会对新网站产生不可预料的影响,所以为了让老用户,和搜索引擎把重心转移到新网站,所以就需要做一个老域名跳转到新域名的操作。 - SEO涉及到一个排名,网站的排名越高,在搜索引擎排名就越靠前,而这个权重是根据网站的页面数量来判定的,搜索引擎认为该网站比较有价值,就会提升该网站权重,而它判断网站是根据域名来的,在搜索引擎眼里,一个网站是根据域名来判断的。 - 若一个网站同时有A、B两个域名可以同时访问,那搜索引擎会认为第二个B域名是一个冒牌货,所以搜索引擎会把高权重给第一个A域名,低权重给第二个B域名。 - 而为了解决这种问题,把一个网站A域名跳转到另一个网站B域名上,提升跳转到的那个域名的权重,并且定义一个301的状态码——>301状态码叫做,永久重定向,永久跳转。 - 永久跳转,搜索引擎它认为A网站不用了,就可以把A网站的权重降下去,然后把所有权重加到B网站上去,这就是301状态码的作用。 ## 域名跳转配置文件介绍 - 加入到配置文件的内容 ``` <IfModule mod_rewrite.c> //需要mod_rewrite模块支持 RewriteEngine on //打开rewrite功能,on就是打开 RewriteCond %{HTTP_HOST} !^111.com$ //定义rewrite的条件,主机名(域名)不是111.com满足条件。 RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行 </IfModule> ``` 1. mod_rewrite模块,在编译apache的时候用到了一个most,most会把大多数模块编译进来;如果没有用most,没有加任何的编译参数,那mod_rewrite模块也不会出现 2. 定义rewrite的条件,主机名(域名)不是111.com满足条件。当域名不是111.com的时候,就会跳转到111.com,也就是说现在以111.com为主域名,它的权重大一点。如果不是 ! 叹号,表示取反的意思。HOST就是定义的域名,111.com就是HOST。!^111.com$,表示以111开头的,到com结尾的,这样定义是为了包含111.com也会匹配 3. 这个^/(.*)$ 理解为: 非域名以外剩下的部分, / 斜杠表示域名后面的斜杠,(.*)就是 / 斜杠后的所有, $表示结束语句 - 凡是以 ^/(.*)$ 域名的 都会跳转到 http://111.com/(.*) - 例如:^/(.*)/([1-9]+)$ 跳转就可以写成 http://111.com/$1/$2 - 这个 R=301,L 在跳转完成后还需要加一个R=301状态码,L 表示只跳转一次结束 - 301是永久重定向 - 302是临时重定向,临时重定向在搜索引擎不太友好,它会认为仅仅是临时的,不过把原来域名的权重降低 ### 域名重定向实验操作 1. 打开虚拟主机配置文件,并修改配置文件 ``` [root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 更改前 <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com #<Directory /data/wwwroot/111.com> <FilesMatch 123.php> AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FilesMatch> #</Directory> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost> 更改后 <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com #<Directory /data/wwwroot/111.com> #<FilesMatch 123.php> # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user # </FilesMatch> #</Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost> 然后保存退出 ``` 2. 检测修改的配置文件是否存在语法错误 ``` [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK ``` 3. 重新加载配置文件 ``` [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful [root@hf-01 ~]# ``` 4. 在测试前,先检查是否加载了rewrite模块 ``` [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite [root@hf-01 ~]# ``` 5. 这里会看到并没有加载rewrite模块,所以还需要到配置文件中打开rewrite模块 6. 如果没有加载,还需要打开rewrite模块,因为在编译的时候指定了most,所以rewrite模块一定存在 ``` [root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf 搜索/rewrite ,找到这一行,并将前面的# 号去掉 LoadModule rewrite_module modules/mod_rewrite.so 然后保存退出 ``` 7. 检测修改的配置文件是否存在语法错误,并重新加载配置文件 ``` [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful [root@hf-01 ~]# ``` 8. 再来查看rewrite模块 ``` [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep rewrite rewrite_module (shared) [root@hf-01 ~]# ``` 9. 查看虚拟主机配置文件 ``` [root@hf-01 ~]# cat /usr/local/apache2.4/conf/extra/httpd-vhosts.conf ``` 10. 开始测试 - curl -x可以使用127.0.0.1也可以是192.168.133.130 进行测试,因为80端口监听的是全网 什么是监听全网 - 全网,就是这台主机上所有的网卡的IP - 80端口默认是:::80,表示监听全网 ``` [root@hf-01 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1525/master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1199/sshd tcp6 0 0 ::1:25 :::* LISTEN 1525/master tcp6 0 0 :::3306 :::* LISTEN 1513/mysqld tcp6 0 0 :::80 :::* LISTEN 2586/httpd tcp6 0 0 :::22 :::* LISTEN 1199/sshd [root@hf-01 ~]# ``` 11. 在虚拟主机中添加2111.com.cn ,然后在重新检查语法错误,在加载配置文件 ``` [root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful ``` 12. 访问2111.com.cn - curl -x192.168.74.150:80 2111.com.cn -I - -I参数,表示不显示内容,只看状态码 ``` [root@hf-01 ~]# curl -x192.168.74.150:80 2111.com.cn -I HTTP/1.1 301 Moved Permanently Date: Wed, 20 Dec 2017 23:32:32 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 Location: http://111.com/ Content-Type: text/html; charset=iso-8859-1 [root@hf-01 ~]# ``` 13. 会看到状态码为301 14. 查看2111.com.cn网站的内容 ``` [root@hf-01 ~]# curl -x192.168.74.150: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://111.com/">here</a>.</p> </body></html> [root@hf-01 ~]# ``` 15. 在域名后面随便添加字符,最后还是跳转到111.com ``` [root@hf-01 ~]# curl -x192.168.74.150:80 2111.com.cn/saasfagssd -I HTTP/1.1 301 Moved Permanently Date: Wed, 20 Dec 2017 23:39:50 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 Location: http://111.com/saasfagssd Content-Type: text/html; charset=iso-8859-1 [root@hf-01 ~]# ``` 16. 去访问随便输入字符的网站,会显示404状态码 - 404状态码,表示这个页面不存在 - 401状态码,表示用户名密码验证的。如果验证不对,就是401状态码;验证对了,就是200状态码 ``` [root@hf-01 ~]# curl -x192.168.74.150:80 http://111.com/saasfagssd -I HTTP/1.1 404 Not Found Date: Wed, 20 Dec 2017 23:42:08 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1 [root@hf-01 ~]# ``` # 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_log ## 访问日志 - 访问日志,就是在浏览器中输入网址,每一次访问,每一次请求,都会生成一个日志 - 查看apache2.4的日志 ``` [root@hf-01 ~]# ls /usr/local/apache2.4/logs/ 111.com-access_log abc.com-access_log access_log httpd.pid 111.com-error_log abc.com-error_log error_log [root@hf-01 ~]# ``` - 查看111.com访问日志 - 日志里面的HEAD都是curl命令导致的 - 日志里面的GET就是不加 -I参数的,在加上-I只会输出状态码,并不会把内容GET下来 - 日志里面包含 来源的IP,时间 , 行为 ,访问的域名 , HTTP的版本1.1 ,状态码 , 大小 ``` [root@hf-01 ~]# ls /usr/local/apache2.4/logs/111.com-access_log /usr/local/apache2.4/logs/111.com-access_log [root@hf-01 ~]# cat !$ cat /usr/local/apache2.4/logs/111.com-access_log 127.0.0.1 - - [20/Dec/2017:23:29:53 +0800] "HEAD HTTP://111.com HTTP/1.1" 200 - 127.0.0.1 - - [20/Dec/2017:23:34:22 +0800] "HEAD HTTP://111.com HTTP/1.1" 401 - 127.0.0.1 - - [20/Dec/2017:23:36:57 +0800] "GET HTTP://111.com HTTP/1.1" 401 381 192.168.202.1 - - [20/Dec/2017:23:42:35 +0800] "GET /favicon.ico HTTP/1.1" 401 381 192.168.202.1 - - [20/Dec/2017:23:42:35 +0800] "GET / HTTP/1.1" 401 381 192.168.202.1 - - [20/Dec/2017:23:42:52 +0800] "GET / HTTP/1.1" 401 381 192.168.202.1 - - [20/Dec/2017:23:48:41 +0800] "GET / HTTP/1.1" 401 381 192.168.202.1 - hanfeng [20/Dec/2017:23:49:04 +0800] "GET / HTTP/1.1" 200 7 127.0.0.1 - hanfeng [20/Dec/2017:23:57:06 +0800] "HEAD HTTP://111.com HTTP/1.1" 200 - 127.0.0.1 - hanfeng [20/Dec/2017:23:59:16 +0800] "HEAD HTTP://111.com HTTP/1.1" 401 - 127.0.0.1 - hanfeng [21/Dec/2017:00:19:07 +0800] "HEAD HTTP://111.com HTTP/1.1" 200 - 127.0.0.1 - hanfeng [21/Dec/2017:00:19:21 +0800] "GET HTTP://111.com HTTP/1.1" 200 7 127.0.0.1 - - [21/Dec/2017:00:19:37 +0800] "GET HTTP://111.com HTTP/1.1" 200 7 127.0.0.1 - - [21/Dec/2017:00:19:41 +0800] "GET HTTP://111.com HTTP/1.1" 200 7 127.0.0.1 - - [21/Dec/2017:00:24:13 +0800] "HEAD HTTP://111.com HTTP/1.1" 200 - 127.0.0.1 - - [21/Dec/2017:00:25:42 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 401 - 127.0.0.1 - hanfeng [21/Dec/2017:00:27:11 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 127.0.0.1 - hanfeng [21/Dec/2017:00:27:43 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 155 127.0.0.1 - hanfeng [21/Dec/2017:00:29:05 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 127.0.0.1 - hanfeng [21/Dec/2017:00:52:40 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 192.168.202.130 - - [21/Dec/2017:21:07:37 +0800] "HEAD HTTP://2111.com.cn HTTP/1.1" 301 - [root@hf-01 ~]# ``` ### 定义日志文件格式 - 上面输出的日志太过简单,不是我们所需要的日志格式,日志其实可以自定义格式的 1. 打开主配置文件 - 默认使用的是common - %h,来源IP - %l,用户 - %u,用户名和密码 - %t,时间 - %r,行为和网站 - %>s,网站状态码 - %b,页面大小 - {Referer}i 表示访问页面的上一个所访问的页面 - %{User-Agent}i 表示用户代理,是通过浏览器访问,还是curl命令访问,最终获得网站的内容,浏览器就是用户代理 ``` [root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf ——>不更改信息 搜索 /LogFormat ,看到的就是文件格式,这里提供了两个文件的格式,默认使用的是common LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common ``` 2. 打开虚拟机配置文件 ``` [root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 未更改前 <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com 2111.com.cn # <Directory /data/wwwroot/111.com> # <FilesMatch 123.php> # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user # </FilesMatch> #</Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost> 更改后,将common 改为 combined <VirtualHost *:80> DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com 2111.com.cn # <Directory /data/wwwroot/111.com> # <FilesMatch 123.php> # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user # </FilesMatch> #</Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" combined </VirtualHost> ``` 3. 检查配置文件是否存在语法错误,并重启配置文件 ``` [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful [root@hf-01 ~]# ``` 4. 使用curl命令访问网址 ``` [root@hf-01 ~]# curl -x192.168.202.150:80 http://111.com123.php -I HTTP/1.1 200 OK Date: Thu, 21 Dec 2017 13:50:10 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@hf-01 ~]# ``` 5. 再用浏览器访问下网址 ![输入图片说明](https://static.oschina.net/uploads/img/201712/21135305_UQVA.jpg "浏览器访问网址") 6. 查看日志文件 - 会看到日志文件丰富了 ``` [root@hf-01 ~]# tail !$ tail /usr/local/apache2.4/logs/111.com-access_log 127.0.0.1 - - [21/Dec/2017:00:24:13 +0800] "HEAD HTTP://111.com HTTP/1.1" 200 - 127.0.0.1 - - [21/Dec/2017:00:25:42 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 401 - 127.0.0.1 - hanfeng [21/Dec/2017:00:27:11 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 127.0.0.1 - hanfeng [21/Dec/2017:00:27:43 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 155 127.0.0.1 - hanfeng [21/Dec/2017:00:29:05 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 127.0.0.1 - hanfeng [21/Dec/2017:00:52:40 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 192.168.202.130 - - [21/Dec/2017:21:07:37 +0800] "HEAD HTTP://2111.com.cn HTTP/1.1" 301 - 192.168.202.130 - - [21/Dec/2017:21:51:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 192.168.202.1 - - [21/Dec/2017:21:51:32 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0" 192.168.202.130 - - [21/Dec/2017:21:55:08 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" [root@hf-01 ~]# ``` 7. 测试{Referer}i ,在论坛新建一个笔记,[测试网址](http://ask.apelearn.com/question/17687),加入自己的测试链接,并在日志中打开自己的测试链接 8. 再来查看日志文件,会看到生成了{Referer}i ``` [root@hf-01 ~]# tail -5 /usr/local/apache2.4/logs/111.com-access_log 192.168.202.130 - - [21/Dec/2017:21:07:37 +0800] "HEAD HTTP://2111.com.cn HTTP/1.1" 301 - 192.168.202.130 - - [21/Dec/2017:21:51:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 192.168.202.1 - - [21/Dec/2017:21:51:32 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0" 192.168.202.130 - - [21/Dec/2017:21:55:08 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 192.168.202.1 - - [21/Dec/2017:22:04:27 +0800] "GET /123.php HTTP/1.1" 200 7 "http://ask.apelearn.com/question/17687" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0" [root@hf-01 ~]# ``` # 扩展 - [apache虚拟主机开启php的短标签](http://ask.apelearn.com/question/5370 )