Apache日志分割
Apache网站日志是指存储在Apache服务器上的网站访问日志数据。这些日志数据包含了网站用户的访问历史和行为,可以帮助开发人员和系统管理员了解网站的性能和健康状况,以及用户的需求和反馈。
通过分析Apache日志数据,可以发现和解决网站的性能问题,并提高网站的用户体验和可靠性。对于SEO优化人员来说,分析网站日志可以分析搜索引擎蜘蛛的规律和网站在SEO方面的问题。
Apache在默认情况下,日志文件始终是一个文件,随着日积月累,文件会越来越大,所以,最好是将日志按日期分割,每天一个日志,这样有利于对日志进行分析。
找到站点/usr/local/apache/conf/vhost/里对应的配置文件.conf,不同的配置路径可能会不同。
默认日志设置一般是:
ErrorLog /mnt/wwwlog/error.log
CustomLog /mnt/wwwlog/access.log combined
ErrorLog对应的是错误日志,CustomLog就是经常要用到的网站的访问日志,搜索引擎爬虫爬行的记录就在这里面。
combined是指日志格式,Apache日志格式有两种,分别为common、combined。
1、通用日志格式(Common Log Format),一种典型的日志记录格式。
LogFormat "%h %l %u %t "%r" %>s %b" common
CustomLog logs/access_log common
2、组合日志格式(Combined Log Format),一般建议用这一种,记录的信息更多更详细,如下所示,多了参数Referer和User-agent,User-Agent字段用于说明是哪个HTTP客户端应用程序在发起已被记录的请求,而Referer字段则提供了更多与请求端在何处找到这个URL的有关信息。
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"" combined
CustomLog log/access_log combined
按日志分割日志,使用Apache自带的轮循工具rotatelogs来对日志文件进行轮循。rotatelogs基本是按时间或大小来控制日志的。我们这里按时间来分割,每天一份日志文件。
注释掉旧的日志配置,添加新的日志配置,建议不要直接删除旧的日志配置,新的配置有可能会出错,如果Apache出现了错误,可以马上先改回旧的配置。
#ErrorLog /mnt/wwwlog/error.log
ErrorLog "|/usr/sbin/rotatelogs /mnt/wwwlog/error_%Y%m%d.log 86400 480"
#CustomLog /mnt/wwwlog/access.log combined
CustomLog "|/usr/sbin/rotatelogs /mnt/wwwlog/access_%Y%m%d.log 86400 480" combined
以上面的配置为例,2020年6月1日的日志文件名将会是access_20200601.log,86400为一天(60秒*60分*24小时),即每天对日志文件进行轮询,重新生成一份日志文件。480是时差,如果以北京时间为准,480为固定值,不用太多理会。
一份完整的网站配置示例:
<VirtualHost *:80>
ServerAdmin example@sina.com
php_admin_value open_basedir /mnt/www/api:/tmp:/var/tmp:/proc
ServerName seo.lichil.com
ServerAlias seo.lichil.com
DocumentRoot /mnt/www/api
<Directory /mnt/www/seo>
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Order Deny,Allow
Require all granted
DirectoryIndex index.php index.html index.htm
</Directory>
#ErrorLog /mnt/wwwlog/error.log
#CustomLog /mnt/wwwlog/access.log combined
ErrorLog "|/usr/sbin/rotatelogs /mnt/wwwlog/error_%Y%m%d.log 86400 480"
CustomLog "|/usr/sbin/rotatelogs /mnt/wwwlog/access_%Y%m%d.log 86400 480" combined
</VirtualHost>