日志配置详解
access_log 配置
http {
# 基本语法
# access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
# 定义日志格式
log_format main '$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 main;
# 带缓冲的日志(提高性能)
access_log /var/log/nginx/access.log main buffer=32k flush=5s;
# 压缩日志
access_log /var/log/nginx/access.log.gz main gzip=9 buffer=32k;
server {
# 站点专用日志
access_log /var/log/nginx/example.access.log main;
# 关闭特定路径的日志
location /health {
access_log off;
return 200 "OK";
}
# 静态资源不记录日志
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
access_log off;
}
}
}
2026/3/20大约 8 分钟