• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Nginx for Newbie: set up log rotation

December 18, 2023

Nginx for Newbie: Set Up Log Rotation

Log rotation is an essential task for any server administrator. It helps manage log files efficiently, preventing them from consuming excessive disk space and becoming unmanageable. In this article, we will explore how to set up log rotation for Nginx, a popular web server and reverse proxy server.

Why Log Rotation is Important

Log files are generated by Nginx to record various events and activities, such as server errors, access logs, and security-related information. Over time, these log files can grow significantly in size, consuming valuable disk space and making it difficult to find relevant information.

Log rotation solves this problem by automatically compressing and archiving log files after a certain period or size threshold is reached. It allows you to keep a manageable number of log files while preserving historical data for analysis and troubleshooting.

Configuring Log Rotation for Nginx

To set up log rotation for Nginx, follow these steps:

Step 1: Install Logrotate

Logrotate is a utility that automates the process of rotating, compressing, and managing log files. It is commonly available on most Linux distributions. If it is not already installed on your server, you can install it using the package manager.

For example, on Ubuntu or Debian, you can install logrotate by running the following command:

sudo apt-get install logrotate

Step 2: Create a Logrotate Configuration File

Next, create a logrotate configuration file specifically for Nginx. This file will define the log files to rotate, the rotation frequency, compression settings, and other options.

Open a text editor and create a new file:

sudo nano /etc/logrotate.d/nginx

Inside the file, add the following configuration:

/var/log/nginx/*.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        /etc/init.d/nginx reload > /dev/null
    endscript
}

This configuration specifies that log files in the /var/log/nginx/ directory should be rotated daily. It keeps the last 7 rotated logs, compresses them, and delays compression until the next rotation. The postrotate section reloads Nginx after rotation to ensure it continues writing logs to the correct file.

Step 3: Test the Logrotate Configuration

Before enabling log rotation, it is essential to test the configuration to ensure it is valid and working correctly. You can do this by running the following command:

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

This command will simulate log rotation and display the output in the terminal. Check for any errors or warnings that may indicate a problem with the configuration.

Step 4: Enable Log Rotation

Once you have verified that the logrotate configuration is correct, you can enable log rotation by running the following command:

sudo logrotate /etc/logrotate.d/nginx

This command will rotate the Nginx log files according to the specified configuration.

Conclusion

Log rotation is a crucial task for managing log files effectively. By setting up log rotation for Nginx, you can ensure that log files do not consume excessive disk space and remain manageable. Remember to regularly check the logrotate configuration and adjust it as needed to meet your specific requirements.

For more information about VPS hosting and how it can benefit your website or application, visit Server.HK.

Recent Posts

  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)
  • VPS Hosting vs Shared Hosting: Why the Upgrade Is Worth It for Asia-Facing Websites
  • Hong Kong VPS vs Google Cloud Asia: Which Delivers Better China Performance in 2026?

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot