In any production environment, diligent monitoring and comprehensive logging are not optional extras; they are fundamental pillars of a robust and resilient Nginx deployment. Without them, troubleshooting issues becomes a guessing game, performance bottlenecks remain hidden, and potential security threats can go unnoticed. This section delves into how to effectively leverage Nginx's monitoring and logging capabilities to keep your high-performance web server running smoothly and securely.
Nginx provides two primary types of logs: the 'access log' and the 'error log'. The access log records every request made to your server, including the client's IP address, the requested URL, the HTTP status code, and the user agent. The error log, on the other hand, is crucial for diagnosing problems. It captures warnings, errors, and critical messages generated by Nginx itself.
http {
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server {
listen 80;
server_name example.com;
...
}
}Customizing the access log format is a powerful way to extract the specific information you need for analysis. Nginx uses 'log formats' to define the structure of log entries. The default format is usually sufficient, but you might want to include additional details like the time taken to process a request or the referrer URL.
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" ';
access_log /var/log/nginx/access.log main;
...
}The error_log directive also allows you to specify a logging level, which controls the verbosity of error messages. Common levels include debug, info, notice, warn, error, and crit. For production, warn or error are generally recommended to avoid excessive log file growth while still capturing important issues.
http {
error_log /var/log/nginx/error.log warn;
...
}Beyond Nginx's built-in logging, there are several advanced monitoring techniques to gain deeper insights into your server's health and performance. The 'stub_status module' provides a simple, real-time overview of Nginx's active connections, requests, and worker processes. This is invaluable for quick status checks.