Beginner Fundamentals

Logging

Logs are your window into what Nginx is doing. Nginx keeps two kinds: an access log for every request and an error log for problems.

access_log

The access log records each incoming request:

access_log /var/log/nginx/access.log;

You can set it per server or per location. To disable logging for a noisy path:

location /health {
    access_log off;
}

error_log

The error log captures startup issues, config problems, and failed requests. It takes a severity level:

error_log /var/log/nginx/error.log warn;

Levels from least to most verbose: crit, error, warn, notice, info, debug. Use warn in production and debug only when troubleshooting.

log_format

You control what the access log records with log_format, defined in the http context:

http {
    log_format main '$remote_addr - $request '
                    '$status $body_bytes_sent '
                    '"$http_user_agent"';

    access_log /var/log/nginx/access.log main;
}

The $variables are filled in per request: $remote_addr is the client IP, $status the response code, and so on.

Reading Logs

Watch traffic live from the terminal:

sudo tail -f /var/log/nginx/access.log

Good logging makes debugging and monitoring far easier.