Nginx cache cleaning configuration to keep website content updated
When building a website, we often use Nginx as a reverse proxy server to accelerate website access and cache static files. However, when we update website content, we need to clear Nginx's cache in time to keep users accessing the latest content. This article will introduce how to configure Nginx cache cleaning to keep website content updated.
1. Configure the cache path
First, we need to configure the cache path of Nginx. Open the Nginx configuration file, usually /etc/nginx/nginx.conf
or /etc/nginx/conf.d/default.conf
, and find http
section, add the following configuration:
http { ... proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; server { ... } ... }
The above configuration stores cache files in the /var/cache/nginx
directory, and sets the cache size to 10GB and the cache expiration time to 60 minutes. Need to be adjusted according to actual situation.
2. Configure the cache cleaning interface
Next, we need to configure an interface to clean the Nginx cache.
In the Nginx configuration file, find the place where the cache needs to be cleared, such as a POST request:
http { ... server { ... location /update { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; # 清理缓存 proxy_cache_purge my_cache GET POST; } ... } ... }
In the above configuration, location /update
is used to match the need to update URL path, such as /update
. The proxy_cache_purge
directive is used to clear the cache, and the parameter GET POST
means to clear the cache of GET and POST requests at the same time. It can be adjusted according to the actual situation.
3. Install the Purge module
The default installation of Nginx does not include the proxy_cache_purge
module, we need to install it manually.
Use the following command to install Nginx’s module management tool:
yum install -y yum-utils
Use the following command to add Nginx’s extension source:
yum-config-manager --add-repo=https://nginx.org/packages/centos/7/x86_64/
Finally, use the following command to install Nginx’s nginx-module-cache-purge
Module:
yum install -y nginx-module-cache-purge
4. Restart the Nginx service
After completing the above configuration, you need to restart the Nginx service to make the configuration take effect. Use the following command to restart Nginx:
systemctl restart nginx
5. How to clear the cache
Now, we can clean the Nginx cache by accessing the cache clearing interface. For example, if we want to clear the cache in the /update
path, we can use the following command:
curl -X POST http://yourdomain.com/update
This will trigger Nginx to clear the cache in the /update
path.
6. Automatically clear the cache
In order to ensure that the website content is updated, we can automatically maintain the consistency between the cache in Nginx and the actual website content by regularly cleaning the cache.
We can use scheduled task tools (such as Cron) to regularly execute cache clearing commands. For example, to set the cache to be cleared every day at 4 a.m., you can use the following command to edit the scheduled task:
crontab -e
Then add the following line of command:
0 4 * * * curl -X POST http://yourdomain.com/update >> /tmp/nginx_cache_clear.log 2>&1
Save and exit the editor, so that every day at 4 a.m. An operation to clear the cache will be triggered, and the execution results will be saved in the /tmp/nginx_cache_clear.log
file.
Summary
Through the above configuration, we can automatically clean up the Nginx cache and keep the website content updated. At the same time, we can also flexibly adjust cache paths and cleaning methods as needed to adapt to different website needs.
The above is the detailed content of Nginx cache cleaning configuration to keep website content updated. For more information, please follow other related articles on the PHP Chinese website!