SoFunction
Updated on 2025-04-14

Specific implementation of Nginx cache cleaning

As an efficient web server and reverse proxy server, Nginx plays a crucial role in providing fast page response and optimizing web performance. Nginx's caching mechanism reduces duplicate processing of data by storing request and response data from back-end servers or clients, thereby greatly improving the system's response speed and throughput. However, as cache data continues to accumulate, it becomes very important to effectively manage and clean caches. A suitable cache cleaning strategy not only frees up disk space, but also ensures the timeliness of cached data and prevents cache penetration and data obsolete issues.

1. Overview of Nginx Caching Mechanism

The Nginx caching mechanism is mainly used to cache response data, thereby accelerating request processing. There are many types of Nginx caches, common ones include:

  • Proxy cache: When Nginx is used as a reverse proxy, the responses obtained from the backend server are cached. This can reduce the load on the backend server and improve the overall response speed of the system.

  • FastCGI Cache: When Nginx handles PHP or other FastCGI applications as a front-end proxy, dynamic content obtained from the FastCGI back-end can be cached.

  • Static file cache: Nginx caches static resources (such as pictures, CSS, JS files) to reduce file reading delays when user requests.

  • HTTP Cache: Includes cache control based on HTTP protocol, such asCache-ControlandExpiresThe use of the head helps Nginx decide whether to cache a response.

With caching, Nginx can significantly increase access speed and reduce the pressure on the backend server, but the management of cache expiration and garbage caches is particularly important.

2. Nginx cache cleaning strategy

The main purpose of cache cleaning is to ensure that the data stored in the cache is up to date, while avoiding the cache from taking up too much disk space. Here are several common cache cleaning strategies:

2.1 Time-based cache expiration strategy

A common cache cleaning strategy is based on the expiration time of cache items. In Nginx, you can control the expiration time of the cache by setting the cache expiration time. When the cache entry expires, Nginx automatically deletes the cache entry and re-pulls data from the backend server on the next request.

2.1.1 Configure cache expiration time

Nginx providesproxy_cache_validDirective to set the validity period of the cache. For example:

http {
    upstream backend {
        server ;
        server ;
    }

    server {
        listen 80;
        server_name ;

        location / {
            proxy_cache backend;
            proxy_cache_valid 200 1h;  #200 The cache validity period of the status code is 1 hour            proxy_cache_valid 404 1m;  #404 The cache validity period of the status code is 1 minute            proxy_pass http://backend;
        }
    }
}

In this configuration:

  • proxy_cache_valid 200 1hAll responses that return 200 status codes are set to survive in the cache for 1 hour.
  • proxy_cache_valid 404 1mAll responses that return a 404 status code are set to survive in the cache for 1 minute.

2.1.2 Configuring the cache cleaning cycle

Nginx's own cache mechanism does not automatically clean up expired cache entries. To avoid the cache directory growing, you can configure a cache cleaning policy. Can be passedproxy_cache_pathIninactiveParameters to set the cache cleaning cycle.

http {
    proxy_cache_path /var/cache/nginx keys_zone=cache_zone:10m max_size=1g inactive=60m;
    
    server {
        listen 80;
        server_name ;

        location / {
            proxy_cache cache_zone;
            proxy_pass http://backend;
        }
    }
}

In this configuration,inactive=60mIndicates that if the cache item is not accessed within 60 minutes, it will be considered invalid and will be deleted.

2.2 Space-based cache cleaning strategy

Another common cleaning strategy is based on the disk space occupied by the cache. When the disk space of the cache directory reaches a certain threshold, Nginx will automatically clean up expired cache files. Nginx bymax_sizeParameters to limit the total size of the cache.

2.2.1 Configuring cache size limits

http {
    proxy_cache_path /var/cache/nginx keys_zone=cache_zone:10m max_size=2g;
    
    server {
        listen 80;
        server_name ;

        location / {
            proxy_cache cache_zone;
            proxy_pass http://backend;
        }
    }
}

In this configuration,max_size=2gIt means that the cache directory can occupy up to 2GB of disk space. When the cache directory exceeds this limit, Nginx will automatically delete the oldest cache item until the cache space falls back to the specified size limit.

2.3 Request-based cache cleaning policy

Sometimes it is very useful to clean caches based on requests or URLs. For example, when certain resources or pages change, the related cache may need to be manually cleaned. Nginx supports deleting specific caches by setting conditions.

2.3.1 Configure cache cleaning command

Through the cache cleaning mechanism that comes with Nginx, the function of cleaning specific caches cannot be directly implemented. However, the specified cache can be deleted through scripts and cache keys. For example, usefindThe command combines the file path to delete a specific cache file.

find /var/cache/nginx/ -name '*example_page*' -exec rm {} \;

This command will delete all withexample_pageRelated cache files. While this approach is not as efficient as automatic cleaning, it provides flexibility for manually managing caches.

2.4 Manual cache cleaning

In some scenarios, system administrators need to manually clean up the Nginx cache. This cleaning method usually involves periodically cleaning cache directories or managing caches through external tools.

2.4.1 Cleaning cache with scheduled tasks

The cache can be cleaned regularly through the cron timing task. Here is an example of cleaning the cache once a day:

0 0 * * * /usr/bin/find /var/cache/nginx/ -type f -mtime +7 -exec rm {} \;

This command deletes cached files in the cache directory that have not been accessed for more than 7 days. By adjusting-mtime +7The parameters in   can control the retention time of the cache.

2.5 Nginx integration with external cache management tools

In some complex applications, external cache management tools may be required to assist with the cleanup of caches. For example, when using Redis, Memcached, or other distributed cache systems, control of cache cleaning can be given to these tools. Nginx integration with these tools can be achieved through proxy and caching mechanisms. For example, you can use Redis to manage cache entries and clean out expired caches in Redis in conjunction with Nginx configuration.

3. Best practices for cache cleaning

3.1 Fine cache strategy

In actual production environments, overly simple cache cleaning strategies may lead to inconsistent cached data or the existence of expired data. Therefore, the best practice is to set different expiration times and cleanup strategies for different types of caches. For example:

  • Static resources (such as images, CSS, JS files) can be cached for a long time.
  • Dynamically generated content (such as API request response) should set a short cache time.

3.2 Automation of cache cleaning

Automated cache cleaning is the key to ensuring the healthy operation of the cache system. By combining Nginx'sinactiveandmax_sizeParameters, combined with operating system-level timing tasks (such as cron), cache cleaning tasks can be performed regularly. Through automated cache management, human errors can be avoided and cache cleaning can be ensured.

3.3 Cooperate with logs and monitoring

Monitoring and logging the cache cleaning process can help operation and maintenance personnel discover problems in a timely manner. For example, you can record the time of cache cleaning, the number of cache files deleted, and other information, and track the cache health status in real time in combination with monitoring systems (such as Prometheus, Zabbix, etc.).

3.4 Avoid cache penetration

Cache penetration refers to requesting to bypass the cache and reaching the backend server directly, usually due to unreasonable cache settings or cache failure. To avoid cache penetration, appropriate cache rules can be used, such as:

  • Generate a unique cache key for each user request to avoid mutual interference with cache requests by different users.
  • Set a reasonable cache expiration time to ensure that the cached data is up to date.

3.5 Configure cache path rationally

Nginx caches are usually stored on disk, and it is important to ensure that the cache directory has enough disk space. Regularly monitor the disk space of the cache directory to avoid full disks being cached, which affects the performance and stability of the system.

4. Frequently Asked Questions and Solutions

4.1 The cache is not cleaned in time

Problem description: Sometimes the cache may not be cleaned in time, causing the user to see expired cache content.

Solution

  • make sureproxy_cache_validandinactiveThe parameters are set properly.
  • Configure the appropriate cache expiration time to avoid cache items not being accessed for a long time.
  • In conjunction with logs and monitoring, check whether the cache cleaning is carried out as expected.

4.2 The cache takes up too much space

Problem description: The cache directory takes up too much disk space, resulting in insufficient disk resources.

Solution

  • usemax_sizeParameters limit the maximum disk usage of the cache directory.
  • Clean out expired caches regularly and configure the cache cleaning cycle reasonably.
  • In conjunction with the cron timing task, perform cache cleaning operations regularly.

4.3 Cache penetration

Problem description: Some requests bypass the cache and directly request to the backend server.

Solution

  • Configure a reasonable cache policy to ensure that different types of requests have different cache policies.
  • Use distributed cache systems such as Redis to manage cached data to avoid cache penetration.

5. Summary

Nginx's cache cleaning strategy is crucial to optimizing the performance of web applications and ensuring system stability. Through a reasonable cache cleaning mechanism, avoiding cache expired data, freeing disk space, and reducing cache penetration can improve the system's response speed and availability. Combining Nginx's built-in caching mechanism and external tools, it can more flexibly control the cache cleaning process and ensure the efficient operation of the service.

This is the end of this article about the specific implementation of Nginx cache cleaning. For more information about Nginx cache cleaning, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!