开启 Nginx 访问日志(access log) 通常只需在配置中启用 access_log 指令即可。下面按常见使用场景说明。
一般官方版本和大多数发行版都默认支持,无需额外模块。
编辑 Nginx 主配置文件,例如:
vim /etc/nginx/nginx.conf
在 http 块中(全局生效):
http {
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;
}
✅ 这样 所有 server 都会记录访问日志。
编辑站点配置,例如:
vim /etc/nginx/conf.d/example.conf
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.access.log main;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
location /health {
access_log off;
}
nginx -t
systemctl reload nginx
或:
nginx -s reload
tail -f /var/log/nginx/access.log
access_log off;serverlogrotate(Nginx 默认已配置)nginx -s reopen如果你愿意,可以告诉我:
我可以直接给你一份可用配置。