192.168.1.1 - - [10/Oct/2023:12:00:00 +0800] "GET /index.html HTTP/1.1" 200 1024
%h:客戶端IP;%t:時間戳;%r:請求行(方法+URL+協議);%s:狀態碼;%b:響應字節數。Referer(來源頁面)和User-Agent(用戶代理)。192.168.1.1 - - [10/Oct/2023:12:00:00 +0800] "GET /index.html HTTP/1.1" 200 1024 "https://ref.com" "Mozilla/5.0"。[Wed Oct 11 15:20:30 2023] [error] [client 192.168.1.1] File does not exist: /var/www/html/test.html
debug(調試)→ emerg(緊急),默認error級別。使用LogFormat指令指定格式,格式字符串由%開頭的變量和普通字符組成。
%{Referer}i:請求頭中的Referer字段;%{User-Agent}i:User-Agent字段。%T:請求處理時間(秒);%D:處理時間(微秒)。LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %T" custom_format。通過CustomLog指令指定日志文件路徑和格式。
CustomLog "/var/log/apache2/access.log" combined。<VirtualHost *:80>
ServerName example.com
CustomLog "/var/log/apache2/example_access.log" custom_format
</VirtualHost>
```。
rotatelogs工具按時間或大小分割日志,避免文件過大。CustomLog "|/usr/sbin/rotatelogs -l /var/log/apache2/access_%Y%m%d.log 86400" combined(按天分割)。SetEnvIf指令過濾特定請求,例如記錄特定IP或User-Agent的日志。mod_log_config未使用時),或使用buffered logs減少磁盤I/O。/var/log/httpd//var/log/apache2/。root或apache用戶,權限為755,避免敏感信息泄露。apachectl configtest或httpd -t檢查語法錯誤。systemctl restart apache2(Debian/Ubuntu)或systemctl restart httpd(CentOS/RHEL)。tail -f /var/log/apache2/access.log查看實時日志,或使用goaccess等工具生成可視化報告。