search
HomeOperation and MaintenanceNginxNginx cache cleaning configuration, update website static resources

Nginx cache cleaning configuration, update website static resources

Jul 05, 2023 pm 05:05 PM
Static resourcesnginx cache cleaningConfiguration update

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

  1. 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;

    }

  2. 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";

  3. Restart the Nginx server to make the configuration file take effect.

2. Update website static resources

  1. Create a directory on the server to store static resources, such as /static.
  2. 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;

    }

  3. 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.

  1. 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
NGINX: An Introduction to the High-Performance Web ServerNGINX: An Introduction to the High-Performance Web ServerApr 29, 2025 am 12:02 AM

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

NGINX vs. Apache: A Look at Their ArchitecturesNGINX vs. Apache: A Look at Their ArchitecturesApr 28, 2025 am 12:13 AM

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX vs. Apache: Examining the Pros and ConsNGINX vs. Apache: Examining the Pros and ConsApr 27, 2025 am 12:05 AM

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache: Understanding the Key DifferencesNGINX and Apache: Understanding the Key DifferencesApr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

NGINX Unit: Key Features and CapabilitiesNGINX Unit: Key Features and CapabilitiesApr 25, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports multiple programming languages ​​and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINX Unit vs. Other Application ServersNGINX Unit vs. Other Application ServersApr 24, 2025 am 12:14 AM

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINX Unit: The Architecture and How It WorksNGINX Unit: The Architecture and How It WorksApr 23, 2025 am 12:18 AM

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

Using NGINX Unit: Deploying and Managing ApplicationsUsing NGINX Unit: Deploying and Managing ApplicationsApr 22, 2025 am 12:06 AM

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool