#server
apachelog
developer
apache2日志相关
打开apache看看
查看日志
# 按下shift+g滚动到底部,按下q退出
sudo less /var/log/apache2/access.log
# 快速查看最近num条
sudo tail -n <num> /var/log/apache2/access.log
当然也可以用finder去看
- 查看隐藏文件:ctrl+H
- 需要sudo:sudo nautilus
apache日志显示真实ip
cloudflare代理的情况下,apache日志中所有ip都是127地址。需要启用mod_remoteip模块,大多数情况下这是系统内置的,只需要启用。
sudo a2enmod remoteip
配置apache2.conf
sudo nano /etc/apache2/apache2.conf
在文件尾部加入
# real ip
RemoteIPHeader CF-Connecting-IP
RemoteIPInternalProxy 127.0.0.1
修改apache日志格式,找到LogFormat,默认那里会有好几个LogFormat和一个CustomLog。修改相关块为
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
LogFormat "%a %l %u %t \"%r\" %>s %b" realip # 这是新加的
CustomLog /var/log/apache2/access.log realip # 修改
完成后重启apache
sudo systemctl restart apache2
旧的访问记录不会更改,推荐用tail来查看是否生效。
日志轮转logrotate
这个功能默认已经打开了,只需要自定义配置
sudo nano /etc/logrotate.d/apache2
几个常用参数
- daily 每日轮转
- rotate
保留最近num天的日志,我设为了9999,这样就都保留了