Nginx cache cleaning configuration, update website static resources
Introduction:
With the development of websites and content updates, in order to improve the access speed and user experience of the website, many websites will use Nginx as a backend to the proxy server. The caching function of Nginx can greatly improve the performance of the website. However, during the process of updating the static resources of the website, we need to manually clear the Nginx cache. This article will introduce how to configure Nginx for cache cleaning and how to automatically update website static resources.
1. Nginx cache clearing configuration
Create a path for receiving cache clearing requests, such as /cache/clear. Add the following configuration to the Nginx configuration file:
location /cache/clear {
allow 127.0.0.1; deny all; proxy_cache_purge CACHE_NAME "$scheme$request_method$host$request_uri"; return 204;
}
Modify the Nginx cache configuration file and add A new cache block named CACHE_NAME. Add the following configuration to this cache block:
proxy_cache_path /path/to/cache levels=1:2 keys_zone=CACHE_NAME:10m max_size=10g inactive=60m use_temp_path=off;
proxy_cache_key "$scheme$request_method$host$request_uri";
2. Update website static resources
In the Nginx configuration file, add a location for processing static resources.
location /static {
root /path/to/static; expires max; add_header Cache-Control public;
}
#!/bin/bash # 静态资源目录 STATIC_DIR="/path/to/static" # 缓存清理URL CACHE_CLEAR_URL="http://localhost/cache/clear" # 进入静态资源目录 cd $STATIC_DIR # 拉取最新的代码 git pull # 清理Nginx缓存 curl -X PURGE $CACHE_CLEAR_URL # 复制静态资源到Nginx的目录 cp -R ./* /path/to/nginx/static # 重启Nginx服务器 service nginx restart
The above script will first switch to the static resource directory, and then pull the latest code through the git command. Then, it will use curl to send a cache clearing request to clear Nginx's cache. Then, it copies the new static resources to Nginx's directory and finally restarts the Nginx server.
Conclusion:
By configuring Nginx's cache cleaning and updating scripts for website static resources, we can easily keep the website's performance and content updated. During the website development process, we can configure and optimize according to the actual situation to improve user experience and website access speed.
The above is the detailed content of Nginx cache cleaning configuration, update website static resources. For more information, please follow other related articles on the PHP Chinese website!