


Nginx cache cleaning configuration, update website static resources
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;
Copy after login}
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";
- Restart the Nginx server to make the configuration file take effect.
2. Update website static resources
- Create a directory on the server to store static resources, such as /static.
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;
Copy after login}
- Configure a script to update the static resources of the website. This can be achieved using shell scripts or other scripting languages. The following is an example script:
#!/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.
- Use scheduled tasks or other methods to automatically execute scripts that update static resources. For example, it can be executed once an hour to ensure that the static resources of the website are always up to date.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The Gin framework is a lightweight, fast, and flexible web framework that allows developers to build high-performance web applications through a simple and beautiful API. In web applications, static resource files (such as images, CSS, JavaScript, fonts, etc.) are usually unchanged, so these resource files need to be processed efficiently to improve application performance. In the Gin framework, processing static resource files is very simple. This article will introduce how to handle static resource files in the Gin framework. 1. In G

Symfony framework middleware: Accelerate the loading and processing of static resources Introduction: In modern web applications, the loading and processing of static resources (such as JavaScript, CSS, and images) is an important link. For websites with a large number of visits, how to load and process static resources efficiently becomes particularly important. Fortunately, the Symfony framework provides middleware to help us speed up the loading and processing of static resources. This article will introduce how to use the middleware function of the Symfony framework to optimize

What should I do if the win7 configuration update is 100% stuck? Some users want to upgrade win7 but they are stuck. They don’t know how to deal with it. Let’s take a look at how to operate it. 1. Click the “Windows” icon in the lower left corner of the desktop. Then click "Run". 2. Enter the "msconfig" command in the run window, and then click "OK" to enter the system configuration. 3. In the system configuration, click the "Services" option, check the box in front of "Hide all Microsoft services (H)" on the lower left, then click "Disable All", and finally click "Apply". 4. Restart the computer to perform Windows update. When the update is completed, you need to restore the programs disabled in step 3. Click "Enable All". That's it

How to improve the static resource access speed of Python website through CDN acceleration technology? With the development of the Internet, the demand for website access speed is getting higher and higher, especially for access to static resources. In order to improve user experience and website performance, using CDN (content delivery network) acceleration technology has become a common choice. This article will introduce how to improve the static resource access speed of Python websites through CDN acceleration technology, and provide you with specific code examples. CDN is a distributed network architecture that connects websites to

How to improve the access speed of Java website through static resource separation? With the rapid development of the Internet, more and more people use websites to obtain information and communicate. For a Java website, access speed is crucial, it directly affects the user experience and the success of the website. Among them, the loading speed of static resources is one of the important factors affecting the website access speed. This article will introduce how to improve the access speed of Java websites through static resource separation. What are static resources? First, we need to clarify what static resources are.

How PHP connects with Tencent Cloud CDN acceleration service to realize static resource acceleration function. With the rapid development of the Internet, website access speed has become one of the important indicators of user experience. In traditional website architecture, the loading speed of static resources is often affected, resulting in slower page loading. In order to solve this problem, Tencent Cloud provides a CDN acceleration service, which can help developers speed up the loading of static resources and improve the website access experience. This article will introduce how to use PHP language to connect to Tencent Cloud CDN acceleration service

Although Microsoft has stopped updating the win7 system, some win7 users who have not updated to the latest version can still update. Recently, some users asked me what to do if the Win7 configuration update fails 100%, so today I will teach you how to solve the problem of Win7 configuration update not working 100%. Let’s take a look below! ! ! Solution to the problem that the win7 configuration update is 100% unresponsive: 1. Press the win key + r key, enter msconfig, and click OK. 2. In the system configuration, click "Services", check the box in front of "Hide all Microsoft services (H)" on the lower left, then click "Disable All", and finally click "Apply". 3. Restart the computer for Wi-Fi

Due to various reasons, after cleaning the C drive in Win7, some netizens will find that the computer fails to restart the Win7 configuration update. So what should I do if win7 fails to restart the configuration update after cleaning the disk? Is it possible to close the interface for restarting the computer to configure the update? The editor below will teach you how to solve the problem that the Win7 restart configuration update fails after cleaning the C drive in Win7. We can turn off the automatic update function of the system to avoid restarting the configuration update. The specific operations are as follows: 1. Restart the computer and press F8 when booting to enter safe mode. 2. Then click on the start icon in the lower left corner to open "Run", then click to enter "services.msc" and press Enter to confirm. 3. Open the "Service" window
