在 Nginx 中开启调试日志(debug log),通常有以下几种方式,取决于你想调试的范围和 Nginx 的编译情况。
Nginx 的 debug 日志级别 只有在编译时开启了 --with-debug 才有效。
nginx -V 2>&1 | grep -- '--with-debug'
编辑 nginx.conf:
error_log /var/log/nginx/error.log debug;
⚠️ 注意:
debug 会输出大量日志server {
listen 80;
server_name example.com;
error_log /var/log/nginx/example_debug.log debug;
location / {
# 正常配置
}
}
或只针对某个 location:
location /api/ {
error_log /var/log/nginx/api_debug.log debug;
}
error_log /var/log/nginx/rewrite.log notice;
rewrite_log on;
rewrite_log on; 会输出 rewrite 过程notice 或 debug只调试特定 IP,避免日志爆炸:
events {
debug_connection 192.168.1.100;
debug_connection 10.0.0.0/8;
}
需配合:
error_log /var/log/nginx/error.log debug;
✅ 适合线上排错
tail -f /var/log/nginx/error.log
--with-debugdebug_connectionworker_processes 1;
error_log /var/log/nginx/error.log debug;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
return 200 "ok";
}
}
}
如果你愿意,可以告诉我:
我可以给你更精确的配置。