How To Configure NGINX Log Rotation

When we run NGINX web server its log files can get very large over time. Luckily, NGINX allows you to rotate log files so that you can automatically create new log files and archive old ones. In this article, we will look at how to configure NGINX log rotation.


What is Log Rotation?

When you run a web server, its log files grow over time and become very large files to manage. As a result, you may run out of server space. This can affect application performance and increase technology costs. Log rotation allows you to automatically create new empty log files, archive current log file, and delete old archives conditionally.


How To Configure NGINX Log Rotation

Here are the steps to configure NGINX log rotation.


1. Install logrotate

Logrotate utility allows you to automate the process of log rotation. Open terminal and run the following command to install it.

Ubuntu/Debian

$ sudo apt-get install logrotate 

CentOS/Redhat/Fedora

#yum install logrotate -y

Also read : How to Fix NGINX Worker Connections not enough


2. Configure Log rotation in NGINX

Logrotate is pre-configured to work with popular web servers like NGINX/Apache.

Nevertheless, we can also manually configure it. Logrotate’s global config file is located at /etc/logrotate.conf but it can be overridden, by placing app-specific or website-specific config files at /etc/logrotate.d/.

The default config file for NGINX server is located at /etc/logrotate.d/nginx which can be used as a template for adding more websites/apps.

/var/log/nginx/*.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 0640 www-data adm
        sharedscripts
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi \
        endscript
        postrotate
                invoke-rc.d nginx rotate >/dev/null 2>&1
        endscript
}

Let us look at it in detail.

weekly – rotate logs once a week

missingok – do not throw error if log file is missing

rotate 52 – keep 52 log files, one for each week of the year. It will delete old files only after keeping on year worth of log files. You can change this to rotate 4 to keep 4 weeks worth of log files instead.

compress – compress old log files to save space

notifempty – ensure that logrotate only writes to new files

create – create new empty log file after each rotation

sharedscripts – logrotate will run only once, no matter how many log files no matter how many log files match its pattern

postrotate/prerotate – postrotate script is run after log rotation. prerotate script is run before log rotation.

Also read : How to Disable ETag in NGINX


3. Manage logrotate

Logrotate will automatically run on your server, once installed. You don’t need to start the service on boot or set up cron jobs for it.

All you need to do is place config files for each app at /etc/logrotate.d/ and logrotate will automatically take care of its log files for you.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!