How To Configure Logging and Log Rotation in Nginx on an Ubuntu VPS
Logging is vital for maintaining the seamless operation of a website when it comes to its management. Nginx is a well-known web server that can manage massive volumes of traffic and is frequently used for high-performance websites. This article explains how to configure logging and log rotation in Nginx on an Ubuntu VPS.
Step 1: Enable Nginx Access Logs
Enabling access logs is the first step in setting up logging for Nginx. Access logs are files that contain the IP address of the client, the time of the request, the requested URL, and the status code issued by the server for each request made to the server. To enable access logs, open the /etc/nginx/nginx.conf configuration file using your preferred text editor.
Find the http
block in the configuration file and add the following code:
http {
...
access_log /var/log/nginx/access.log;
...
}
Save the changes and exit the text editor.
Step 2: Configure Nginx Logging
Enabling access logs is the first step in setting up logging for Nginx. Access logs are files that contain the IP address of the client, the time of the request, the requested URL, and the status code issued by the server for each request made to the server. To enable access logs, open the /etc/nginx/nginx.conf configuration file using your preferred text editor.
http {
...
access_log /var/log/nginx/access.log combined;
...
}
The combined argument specifies a predefined log format consisting of the referrer and user agent.
Additional helpful log formats include:
- main: The default format that comprises the request time, status code, and response size.
- The json format logs request and response as JSON objects.
Choose the appropriate log format and save the modifications to the configuration file.
Step 3: Configure Log Rotation
Log files can become fairly large and use important disk space over time. To avoid this from occurring, log rotation should be configured. Log rotation entails relocating old log files to a backup location and producing fresh log files.
Using your preferred text editor, create a new file named nginx in the /etc/logrotate.d/ directory to set log rotation. Add the code shown below:
/var/log/nginx/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
systemctl reload nginx
endscript
}
This code instructs logrotate:
- Rotate logs weekly
- Ignore missing log files
- Maintain 52 weeks of rotational logs
- Compress logs that have been rotated.
- Postpone compression until the subsequent rotation.
- Do not rotate the log file if it is empty.
- Create new log files with the 0640 permission and www-data and adm group ownership.
- Reload Nginx upon log rotation.
Save the file’s modifications and quit the text editor.
Step 4: Test Logging and Log Rotation
Once that logging and log rotation have been configured, it is time to verify their functionality. To test logging, send a request to your Nginx server and examine the log file’s contents. The log file should have a new item containing details about the current request.
To test log rotation, wait until the following log rotation interval (one week in this case) and examine the log file’s contents. The log file should have been relocated to a backup location, and a fresh log file should have been generated.
Best Practices for Nginx Logging and Log Rotation
Best practices for configuring logging and log rotation in Nginx are as follows:
- Pick a format for your logs that incorporates the necessary data without containing sensitive information.
- Use log rotation to avoid log files from becoming excessively huge and consuming precious disk space.
- Configure log file monitoring in order to receive notifications if log files stop being written to or if disk space is running low.
- Review the log files frequently to discover and resolve issues.
- Try employing a log analysis tool to assist you interpret the log data and obtain insight into the traffic and performance of your website.
By adhering to these best practices, you can ensure that your Nginx server is effectively logging and cycling logs.
Final Thoughts
Setting logging and log rotation in Nginx is a crucial aspect of website management. By following the instructions indicated in this article, your Nginx server should now have proper logging and log rotation. Ensure that your server runs smoothly and efficiently by adhering to best practices around logging and log rotation.
FAQs
What is Nginx?
Nginx is a reverse proxy server and web server. It can serve as a load balancer, a content cache, and a web server simultaneously. Nginx is renowned for its speed, dependability, and resource efficiency.
Why is logging important in Nginx?
Logging is essential in Nginx since it enables you to track website traffic, find mistakes and issues, and fix issues. Without tracking, it might be challenging to detect issues and comprehend how your website is being utilized.
What is log rotation?
Log rotation is the process of archiving and/or removing log files in order to prevent them from becoming excessively large and consuming valuable disk space. Log rotation is required because log files can expand rapidly, particularly on websites with significant traffic, and can absorb all available disk space if left unchecked.
What is a log format?
A log format is a specified format for log messages. It sets the information to be included in each log message as well as its format. The Combined Log Format and the Common Log Format are common log formats.
What is a log file?
A log file is a file that contains the log messages that an application or server generates. The log file for Nginx contains information regarding website traffic, problems, and other events. Log files are an essential management and troubleshooting tool for servers and applications.
What is a text editor?
A text editor is a piece of software used to generate, modify, and manipulate plain text files. Text editors are frequently used to modify source code, configuration files, and other text-based documents. Vim, Emacs, and Atom are well-known text editors.
References
- Nginx Logging and Monitoring
- How To Configure Logging and Log Rotation in Nginx on an Ubuntu VPS
- Apache HTTP Server Log Files
Conclusion
Nginx is a robust web server and reverse proxy server used by numerous websites to manage excessive traffic and enhance performance. Logging and log rotation are key components of maintaining a Nginx server since they allow you to track website traffic, find mistakes and problems, and fix issues. By following the methods indicated in this article and sticking to best practices, you can ensure that your Nginx server is effectively logging and cycling logs.