Nginx配置日志管理的最佳实践包括以下几点:
log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log custom;
logrotate工具定期分割日志文件,防止日志文件过大。/var/log/nginx/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 root adm
}
error_log和access_log的级别,例如生产环境中通常设置为warn或error。error_log /var/log/nginx/error.log warn;
chmod 640 /var/log/nginx/*.log
chown root:adm /var/log/nginx/*.log
access_log /var/log/nginx/access.log custom buffer=32k flush=30s;
access_log /var/log/nginx/access.log custom async;
以下是一个综合了上述最佳实践的Nginx配置示例:
http {
log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log custom buffer=32k flush=30s;
error_log /var/log/nginx/error.log warn;
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html index.htm;
}
}
}
通过遵循这些最佳实践,可以有效地管理和维护Nginx日志,确保系统的稳定性和安全性。