Last updated on June 13th, 2024 at 09:20 am
NGINX is a popular web server used to host many high traffic websites and applications. It is a favorite for administrators. Log management is an important task for every website administrator. When we run NGINX web server its log files can get very large over time. This can make things difficult for administrators. 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 eventually become very large to manage. As a result, you may run out of server disk space. This can affect application performance and increase storage costs. In fact, it will also affect performance of other applications running on the server since they will all compete for disk space. A common solution to this problem is Log rotation, whereby you archive a log file when it exceeds a specific size threshold, and replace it with a new file. There are many third party log rotation programs that help you do this. Log rotation allows you to automatically create new empty log files, archive current log file, and delete old archives conditionally. It simplifies life for website administrators who need to manage log files for debugging purposes.
How To Configure NGINX Log Rotation
There are many third party utilities to do log rotation. We will basically install logrotate command in Ubuntu and then point it to the log files of NGINX. Here are the steps to configure NGINX log rotation.
1. Install logrotate
Logrotate utility allows you to automate the process of log rotation. It allows you to control the number & size of log files for various processes running on your server by automatically compressing, archiving & deleting old log files.
Open terminal and run the following command to install it.
Ubuntu/Debian
$ sudo apt-get install logrotate
CentOS/Redhat/Fedora
#yum install logrotate -y
2. Configure Log rotation in NGINX
Logrotate is pre-configured to work with popular web servers like NGINX/Apache. It consists of 2 configuration files. Logrotate’s global config file is located at /etc/logrotate.conf contains all main settings of logrotate program. 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 or /etc/logrotate.d/nginx-rc which can be used as a template for adding more websites/apps. This configuration basically consists of paths to log file directories along with keyword-based directives placed within {…}. It describes log file location, frequency of rotation, number of copies to be retained, and actions before/after deletion.
If you open it, this is what it looks like.
/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.
If you have used default installation the logrotate should be able to easily pick up the NGINX log files from their default location.
To make logrotate rotate logs of other applications, place separate log files in /etc/lograte.d or add more log file paths in the same file, for other applications. As mentioned earlier, each config file is nothing but a collection of log file paths with keyword-based instructions enclosed within {…}.
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.
You can also run logrotate manually with the following command.
logrotate [options] config_file_location
Here is an example to run logrotate with the config file at /etc/logrotate.d.
logrotate /etc/logrotate.d
Conclusion
It is very important to carefully manage log files on your web servers so that they do not get out of control and take up a lot of disk space. Software development teams often need to look into old logs for troubleshooting and debugging purposes. So it is essential to properly archive old logs. Logrotate is a great software that allows you to easily automate log rotation. It works not only with web server but with any process that generates log files. You just need to include its path in lograte config file, and it will take care of the rest.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!
Also read :
How to Fix NGINX Worker Connections not enough
How to Disable ETag in NGINX
How to Fix URI Request Too Large Error in Apache
Apache Restrict Access to URL By IP
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.