Backend Development 9 min read

Configuring Nginx Access and Error Logs, Custom Log Formats, and Log File Caching

This article explains how to configure Nginx access_log and error_log directives, customize log formats with log_format, and improve performance using open_log_file_cache, providing syntax details, parameter meanings, and practical examples.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Configuring Nginx Access and Error Logs, Custom Log Formats, and Log File Caching

Nginx logs are essential for statistics and troubleshooting. There are two main log types: access_log (access logs) and error_log (error logs). Access logs record client requests, while error logs capture problems during request processing.

Setting access_log

The access_log directive records each client request, including IP, browser, referer, request time, URL, etc. The log format can be customized with the log_format directive.

Syntax

access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
path – location of the log file. format – name of the log format (default is combined ). buffer – size of the write buffer (default 64k). gzip – compress log before writing (level 1‑9, default 1). flush – time after which buffered data is flushed. if – condition that determines whether a request is logged.

The special value off disables logging in the current scope.

Scope

access_log can be used in http , server , location , and limit_except contexts.

Basic Usage

access_log /var/logs/nginx-access.log

Uses the default combined format.

access_log /var/logs/nginx-access.log buffer=32k gzip flush=1m

Specifies a 32k buffer, enables gzip compression, and flushes every minute.

Customizing Log Format with log_format

The predefined combined format can be overridden by defining a new format:

log_format combined '$remote_addr - $remote_user [$time_local] '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';

To create a custom format, use:

Syntax

log_format name [escape=default|json] string ...;
name – identifier used in access_log . escape – encoding of variables (default or json). string – the actual format string, which may contain Nginx variables.

Example of defining and using a custom format:

access_log /var/logs/nginx-access.log main
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

A sample log line produced by this format is shown in the article.

Setting error_log

The error_log directive records server and request processing errors.

Syntax

error_log file [level];

Default level is error . Levels include debug, info, notice, warn, error, crit, alert, emerg.

Basic Usage

error_log /var/logs/nginx/nginx-error.log

Can be placed in http , server , location , etc.

open_log_file_cache

When log file paths contain variables, opening and closing files for each request can be costly. The open_log_file_cache directive caches file descriptors.

Syntax

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
max – maximum number of cached descriptors. inactive – time after which unused descriptors are closed (default 10s). min_uses – minimum accesses within inactive to keep cached (default 1). valid – interval to re‑check file names (default 60s). off – disables caching.

Basic Usage

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

Can be set in http , server , or location contexts.

Summary

By configuring access_log and error_log , using log_format for custom log structures, and optionally enabling open_log_file_cache when log paths contain variables, you can efficiently collect and manage Nginx logs. For a full list of log variables, refer to the official Nginx documentation.

BackendconfigurationLoggingnginxerror_logaccess_loglog format
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login 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.