Master Nginx Access and Error Log Configuration for Effective Debugging
This guide explains how to configure and read Nginx access and error logs, covering log format definitions, file locations, log level settings, and practical commands for parsing log entries to troubleshoot server and application issues.
Configure Nginx Access Log
When handling client requests, Nginx writes a new record to the access log, which includes a timestamp and various details about the client and requested resource. The log_format directive defines the log format, and the access_log directive enables the log and sets its file location and format.
The basic syntax for access_log is: access_log log_file log_format; Here log_file is the full path to the log file and log_format is the format name defined in the http, server, or location context. By default, the global http block defines the access log format.
For better maintainability, it is recommended to set a separate access log file for each server block, which overrides the global setting.
If no format is specified, Nginx uses the predefined combined format:
log_format combined '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';You can define a custom format, for example:
log_format custom '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';To use the custom format, specify its name after the log file: access_log /var/log/nginx/access.log custom; If disk space or performance is a concern, you can disable the access log by setting the directive to off:
access_log off;Configure Nginx Error Log
The error_log directive enables and sets the location and severity level of the error log. It can be placed in the http, server, or location context.
Syntax: error_log log_file log_level; Log levels (from low to high severity): debug, info, notice, warn, error, crit, alert, emerg. If no level is specified, the default is error. Each level includes messages of higher severity.
By default, the main nginx.conf defines the error log in the http block:
http {
...
error_log /var/log/nginx/error.log;
...
}It is advisable to set a separate error log file for each server block, which overrides the global setting. For example, to set the error log level to warn for domain.com:
http {
...
error_log /var/log/nginx/error.log;
...
server {
server_name domain.com;
error_log /var/log/nginx/domain.error.log warn;
...
}
}After any configuration change, reload Nginx to apply the changes.
Log File Locations
On most Linux distributions (Ubuntu, CentOS, Debian), the default locations for access and error logs are under /var/log/nginx.
Reading and Understanding Nginx Log Files
You can use standard commands such as cat, less, grep, cut, and awk to open and parse log files. An example access log entry in the default combined format:
192.168.33.1 - - [15/Oct/2019:19:41:46 +0000] "GET / HTTP/1" 200 396 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536 (KHTML, like Gecko) Chrome/38120 Safari/536"The fields represent client IP, remote user, timestamp, request line, status code, response size, referrer, and user agent. You can use tail -f access.log to watch logs in real time.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
