Logging #
Logs are our eyes and ears on the server. Without a well-configured logging system, debugging and troubleshooting activities in production environments become very difficult, like finding a needle in a haystack.
Nginx produces two main types of logs by default:
- Access Log: Records every incoming HTTP request processed by the server.
- Error Log: Records operational problems, failures, and warnings from inside the server.
In this section, we’ll learn how to configure both log types, design custom log formats (like JSON formats for modern log aggregator integration), implement distributed tracing with request IDs, and manage log files using the log rotation utility so they don’t fill up our disk storage space.
Nginx Logging Learning Map #
Here’s the learning flow we’ll go through to master the Nginx logging system:
flowchart TD
Start("Start Learning Logging") --> AccessLog["01. Access Log<br/>(Request Recording & Buffering)"]
Start --> ErrorLog["02. Error Log<br/>(Failure Recording & Severity)"]
AccessLog --> CustomFormat["03. Custom Log Formats<br/>(Performance, JSON & Request ID)"]
ErrorLog --> CustomFormat
CustomFormat --> LogRotation["04. Log Rotation<br/>(Logrotate, USR1 & Disk Space)"]
LogRotation --> End("Ready for Production Implementation")
classDef default fill:#f9f9f9,stroke:#d1d5db,stroke-width:1px,color:#111827;
classDef nodeStyle fill:#eff6ff,stroke:#3b82f6,stroke-width:2px,color:#1e3a8a;
class AccessLog,ErrorLog,CustomFormat,LogRotation nodeStyle;Logging Article List #
For easier navigation, here’s an overview table of the material covered in this section:
| Article | Main Description | Important Topics |
|---|---|---|
| Access Log | Understand recording of incoming HTTP request activity. | The access_log directive, the default combined format, disabling logs for static assets/healthchecks, and log buffering. |
| Error Log | Understand recording of internal problems and failures. | The error_log directive, 8 severity levels, dissecting error log anatomy, troubleshooting common error codes, and the per-IP debug connection feature. |
| Custom Log Formats | Design log structures beyond standard formats. | The log_format directive, upstream performance visualization, JSON format (escape=json), request ID tracing, and application header recording. |
| Log Rotation | Manage long-term log storage automatically. | File descriptor release, the USR1 signal (nginx -s reopen), /etc/logrotate.d/nginx configuration, the delaycompress option, and manual testing. |