Nginx是一個高性能的HTTP和反向代理服務器,它通過日志記錄來幫助管理員跟蹤服務器性能、識別問題和進行故障排除。Nginx的日志主要分為兩種:訪問日志(access log)和錯誤日志(error log)。以下是Nginx日志級別設置指南:
access_log
指令,可以指定日志文件的路徑和格式。例如:http {
access_log /var/log/nginx/access.log combined;
}
log_format
指令定義日志的格式。例如: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;
}
error_log
指令,可以指定錯誤日志的路徑和日志級別。例如:http {
error_log /var/log/nginx/error.log warn;
}
debug
、info
、notice
、warn
、error
、crit
、alert
、emerg
。未指定log_level
參數時,默認為error
。如果不需要訪問日志,可以將其設置為off
以節省磁盤空間和提高服務器性能。
通過合理設置Nginx的日志級別,可以在保證服務器安全的同時,有效地利用日志信息進行故障排查和性能優化。