Apache访问日志
Apache访问日志
在浏览器中访问了某网站,或者在网站页面执行了某些命令,会被记录在访问日志中(log)。默认的log目录为/Apache/log/
了解并配置访问日志
1.通过配置文件了解访问日志
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
//combined和common表示日志格式,默认时common
//%h表示来源ip,%l表示login的用户,%u表示用户,%t表示时间,%r表示行为(get或者head),%s表示状态码,%b表示的大小,%{Referer}i表示浏览器上一次访问的地址,%{User-Agent}i表示访问时使用的方式
2.配置虚拟主机文件
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache2.4/docs/111.com"
ServerName www.111.com
ServerAlias 111.com
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" combined
</VirtualHost>
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
3.通过curl和网页访问查看访问日志的变化
[root@antong extra]# curl -xlocalhost:80 -I 123.com
HTTP/1.1 301 Moved Permanently
Date: Wed, 04 Aug 2021 17:42:42 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.30
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1
[root@antong ~]# tail -n 1 /usr/local/apache2.4/logs/111.com-access_log
::1 - - [04/Aug/2021:13:45:40 -0400] "HEAD HTTP://123.com/ HTTP/1.1" 301 - "-" "curl/7.29.0"
//使用电脑浏览器访问Apache的地址
[root@antong ~]# tail -n 1 /usr/local/apache2.4/logs/111.com-access_log 192.168.200.1 - - [04/Aug/2021:13:47:03 -0400] "GET / HTTP/1.1" 301 223 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
访问日志不记录静态文件
访问日志会把每次访问都记录下来,会占用内存。为了减轻磁盘压力,不记录CSS和img文件。
1.配置虚拟主机文件
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache2.4/docs/111.com"
ServerName www.111.com
ServerAlias 111.com
SetEnvIf Request_URI ".*\.gif$" img //指定类型加入到img变量中
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" combined env=!img //img变量中的类型都不记录
</VirtualHost>
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
2.为网站上传一张图片
cd /usr/local/apache2.4/docs/www.111.com/
mkdir images
[root@antong www.111.com]# ls images/
img.jpg
3.进行测试
[root@antong www.111.com]# curl -xlocalhost:80 www.111.com/images/img.jpg -I HTTP/1.1 200 OK
Date: Wed, 04 Aug 2021 18:07:12 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.30
Last-Modified: Wed, 04 Aug 2021 18:02:13 GMT
ETag: "ede7-5c8bf9a161340"
Accept-Ranges: bytes
Content-Length: 60903
Content-Type: image/jpeg
[root@antong ~]# tailf /usr/local/apache2.4/logs/www.111.com-access_log
::1 - - [04/Aug/2021:14:10:33 -0400] "HEAD HTTP://www.111.com/images/img.jpg HTTP/1.1" 200 - "-" "curl/7.29.0" //注释SetEnvIf使用curl进行访问记录日志
//使用SetEnvIf时使用curl进行访问没有记录日志
访问日志切割
日志一直记录总有一天会把整个磁盘占满,所以有必要让它自动切割,并删除老的日志文件 。
1.配置虚拟主机文件
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache2.4/docs/111.com"
ServerName www.111.com
ServerAlias 111.com
SetEnvIf Request_URI ".*\.gif$" img //指定类型加入到img变量中
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
ErrorLog "logs/111.com-error_log"
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img //rotatelogs是apache自带的日志切割工具,-l参数当前系统的时间来切割
</VirtualHost>
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
2.进行测试
[root@antong www.111.com]# curl -xlocalhost:80 www.111.com -I
HTTP/1.1 200 OK
Date: Wed, 04 Aug 2021 18:22:08 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.30
Last-Modified: Wed, 04 Aug 2021 16:57:52 GMT
ETag: "c-5c8beb3f831f7"
Accept-Ranges: bytes
Content-Length: 12
Content-Type: text/html
[root@antong ~]# ls /usr/local/apache2.4/logs/ //会产生新命名的日志文件
111.com-access_log abc.com-access_log error_log
111.com-error_log abc.com-error_log httpd.pid
123.com-access_20210804.log access_log www.111.com-access_log