Master Nginx Access and Error Log Configuration for Faster Debugging

This guide explains how to configure Nginx access and error logs, customize log formats with log_format, manage log files using access_log and error_log directives, and automate daily log rotation with a Bash script and cron job.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Nginx Access and Error Log Configuration for Faster Debugging

Nginx logs are essential for statistics and troubleshooting; they consist of access_log (request logs) and error_log (error logs). Access logs capture client IP, browser, request time, etc., while error logs record failure details to help locate problems.

1. Access Log Configuration

The log_format directive defines the layout of each log entry. Example:

log_format main '$server_name $remote_addr - $remote_user [$time_local] "$request" '
                '$status $uptream_status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for" '
                '$ssl_protocol $ssl_cipher $upstream_addr $request_time $upstream_response_time';

Key variables:

$server_name : virtual host name.

$remote_addr : client IP address.

$remote_user : authenticated user name (blank if none).

[$time_local] : request time with timezone.

$request : request URI and HTTP protocol.

$status : HTTP status code.

$uptream_status : upstream status code.

$body_bytes_sent : size of response body.

$http_referer : referring page.

$http_user_agent : client browser info.

$http_x_forwarded_for : original client IP when behind a reverse proxy.

$ssl_protocol and $ssl_cipher : SSL details.

$upstream_addr : upstream server address.

$request_time and $upstream_response_time : timing information.

Note: log_format must be placed inside the http block, otherwise Nginx will warn:

nginx: [warn] the "log_format" directive may be used only on "http" level in /etc/nginx/nginx.conf:9

The access_log directive specifies the log file path, format, buffer size, gzip compression, flush interval, and conditional logging:

# Default combined format
access_log /var/logs/nginx-access.log;
# Custom format with buffer and gzip
access_log /var/logs/nginx-access.log buffer=32k gzip flush=1m;
# Disable logging
access_log off;

Parameters:

path : log file location.

format : log format name (default is combined).

buffer : write buffer size (default 64k).

gzip : compress log before writing (level 1‑9, default 1).

flush : time after which buffer is flushed.

if : condition to skip logging.

2. Error Log Configuration

Error logs record failures and cannot be customized in format. The syntax is: error_log path level; path is the file location, and level can be debug, info, notice, warn, error, or crit (default error). error_log logs/error.log info; To effectively disable error logging, redirect it to /dev/null instead of using off: error_log /dev/null; The error_log directive can be placed in main, http, mail, stream, server, or location contexts.

3. Log Rotation

A Bash script can copy current logs with a date suffix and truncate the original files:

#!/bin/bash
LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
cp ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log && >${LOGS_PATH}/access.log
cp ${LOGS_PATH}/error.log ${LOGS_PATH}/error_${YESTERDAY}.log && >${LOGS_PATH}/error.log

Schedule the script with cron to run daily at midnight:

0 0 * * * /usr/local/nginx/logs/NginxLogRotate.sh

This setup ensures Nginx creates a new log file each day while preserving previous logs for analysis.

Nginx log configuration diagram
Nginx log configuration diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendloggingNginxlog rotationerror logAccess Log
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.