logsLinuxVPS

How To Configure Logging and Log Rotation in Nginx on an Ubuntu VPS

Configure logging and log rotation in Nginx on an Ubuntu VPS involves setting up Nginx to log requests and errors, and then configuring a log rotation mechanism to manage log file sizes and retention.

Prerequisites

To follow this tutorial, you will need:

  • One Ubuntu 22.04 server with a non-root sudo-enabled user with a firewall. Follow our Initial Server Setup to get started.
  • Nginx installed on the server. Follow our How to Install Nginx on Ubuntu 22.04 tutorial to get it installed.

With Nginx running on your Ubuntu 22.04 server, you’re ready to begin.

Here’s a step-by-step guide to Configure Logging and Log Rotation in Nginx :

Step 1: Configure Nginx Logging

1.Access Nginx Configuration: Open the Nginx configuration file for editing. This file is usually located in /etc/nginx/nginx.conf or in a specific site configuration file in /etc/nginx/sites-available/

sudo nano /etc/nginx/nginx.conf

2.Configure Access and Error Logs: Inside the http block, you can specify the location and format of the access and error logs:

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;

# Other configurations...
}

access_log specifies the file where access logs are stored and the log format.

error_log specifies the file where error logs are stored and the log level.

3.Save and Exit: Save the changes and exit the text editor.

Step 2: Configure Log Rotation in Nginx

Log rotation is usually managed by the logrotate utility on Ubuntu.

  1. Create a Logrotate Configuration File for Nginx: Create a new logrotate configuration file for Nginx in /etc/logrotate.d/.
sudo nano /etc/logrotate.d/nginx

Add Log Rotation Rules:

Add the following configuration to manage the rotation of Nginx logs:

/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
  • daily rotates logs daily.
  • missingok ignores errors if the log file is missing.
  • rotate 14 keeps 14 days’ worth of logs.
  • compress compresses the rotated logs to save space.
  • delaycompress delays compression until the next rotation cycle.
  • notifempty skips rotation if the log file is empty.
  • create sets permissions for the new log file.
  • postrotate contains commands to run after the log file is rotated. The kill -USR1 command tells Nginx to reopen its log files.

Save and Exit: Save the changes and exit the text editor.

Step 3: Test and Apply the Configuration

  1. Test Nginx Configuration: Before applying the changes, test the Nginx configuration to ensure there are no errors.
sudo nginx -t
  • If the configuration test is successful, you will see a message like syntax is okay and test is successful.
  • Restart Nginx: Restart Nginx to apply the changes.
sudo systemctl restart nginx

Test Log Rotation: You can manually test log rotation to ensure it’s working correctly.

sudo logrotate -f /etc/logrotate.d/nginx

This command forces logrotate to perform a rotation. After running this command, check the log directory to verify that the logs have been rotated.

This section tells Nginx to reload the log files once the rotation is complete.

Conclusion

Proper log configuration and management can save you time and energy in the event of a problem with your server. Having access to the information that will help you diagnose a problem can be the difference between a trivial fix and a persistent headache.

It is important to keep an eye on server logs in order to maintain a functional site and ensure that you are not exposing sensitive information. This guide serves only as an introduction to your experience with logging. You can learn more general tips in our tutorial on How to Uninstall or Remove Package in Ubuntu.