Support Online
Skip to main content

Nginx Logging and Log Rotation Configuration

What will you learn in this guide?

This guide explains how to structure Nginx logs and rotate them to prevent disk overflow.
You will learn to make sense of error and access logs and automation with logrotate.

Technical Summary

Main topic: Nginx logging and log rotation
Solved problem: Uncontrollably growing log files and insufficient error tracking
Gain: More secure, traceable and sustainable server management

Steps followed:

  1. Configure error_log and log levels
  2. Understand how to use access_log and log_format
  3. Turn logging on, off and optimizing
  4. Understand the logic of manual log rotation
  5. Providing automatic log management with logrotate

1. Understanding the error_log Directive

Nginx manages error logs with the error_log directive.
This structure makes it easier to detect problems in the system.

error_log syntax

error_log /var/log/nginx/error.log error;
  • This setting writes records at error level and above to the file.

Log levels

1. emerg: System is inoperable

2. alert: Immediate intervention required

3. crit: Critical error occurred

4. error: An operation failed

5. warn: Unusual but not critical

6. notice: Informative event

7. info: General information

8. debug: Detailed debugging

  • All records at the specified level and above are logged.

  • Turn off logging completely

error_log /dev/null crit;
  • This structure only ignores critical errors.

2. access_log and HttpLogModule

  1. access_log records incoming requests.
  2. This directive provides customizable formats.
  • log_format definition

log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
  • This definition creates the commonly used combined log format.

access_log usage


access_log /var/log/nginx/access.log combined;
  • This setting writes access logs with the specified format.

Turn off logging


access_log off;
  • This method completely disables access logs.

3. Manual Log Rotation Logic

  1. As log files grow larger, disk space is at risk.
  2. Manual rotation provides solution by renaming files.

Moving the log file


mv /var/log/nginx/access.log /var/log/nginx/access.log.0
  • This command archives the current log.

  1. Sending log refresh signal to Nginx

kill -USR1 `cat /var/run/nginx.pid`
  • This command tells Nginx to use the new log file.

4. Automatic Log Rotation with logrotate

  1. Logrotate comes by default on Ubuntu systems.
  • There is a ready-made rotation file for Nginx.

turn on logrotate configuration


sudo nano /etc/logrotate.d/nginx
  • This file determines when and how Nginx logs are returned.

Important settings

  1. Daily rotation is done

  2. 52 old logs are stored

  3. Nginx logs are refreshed after rotation


postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
endscript
  • This section sends a signal to Nginx after rotation.

Frequently Asked Questions (FAQ)

1. Logs are growing very fast, what should I do? Simplify access_log format or use buffer.

2. Is the debug level suitable for production? No, it should only be used in test environments.

3. How do I test if logrotate is not working? Use the command logrotate -d /etc/logrotate.d/nginx.

4. Can I move the logs to a different disk? Yes, you can change the access_log and error_log paths.


Result

Accurate logging allows you to solve problems quickly. Log rotation protects disk security. This structure is indispensable for production environments.

Manage your Nginx-based projects safely on the GenixNode infrastructure.