search
HomeOperation and MaintenanceNginxNginx reverse proxy cache refresh configuration and update website static resources in real time

Nginx reverse proxy cache refresh configuration, real-time update of website static resources

Introduction:
In Web development, static resources are an indispensable part of the website. However, static resources that are frequently updated may cause users to see old versions during access or load slowly. An effective way to solve this problem is to use Nginx's reverse proxy caching function and configure it to update the static resources of the website in real time. This article will introduce how to achieve this function through Nginx configuration.

  1. Enable reverse proxy cache:
    First, we need to enable reverse proxy cache in Nginx configuration. Find the Nginx configuration file (usually nginx.conf) and open it, then find the http section and add the following configuration:

    http {
      proxy_cache_path /path/to/cache/directory levels=1:2 keys_zone=cache_zone:10m max_size=10g inactive=60m;
      proxy_temp_path /path/to/temporary/directory;
      proxy_cache_key "$scheme$request_method$host$request_uri";
      proxy_ignore_headers "Cache-Control" "Expires";
      proxy_cache_valid any 10m;
      proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
      
      server {
     ...
     location / {
       proxy_cache cache_zone;
       proxy_pass http://backend-server;
     }
     ...
      }
    }

    Description:

  2. proxy_cache_path: Set the reverse proxy cache path and related configurations. levels is the directory level, keys_zone is the name and size of the cache area, max_size sets the maximum size of the cache, inactive specifies the cache timeout .
  3. proxy_temp_path: Set the temporary directory path.
  4. proxy_cache_key: Variable used to generate cache key, $scheme represents the request protocol, $request_method represents the request method, $host represents the host name of the request, and $request_uri represents the URI of the request.
  5. proxy_ignore_headers: Specify the response headers that need to be ignored.
  6. proxy_cache_valid: Set the cache validity time.
  7. proxy_cache_use_stale: Defines the circumstances under which stale cache is allowed to be used when updating the cache.
  8. Updating static resources in real time:
    To achieve real-time updating of static resources on the website, we need to add additional logic in the background program or script to issue a request to refresh the cache after each update of the static resources. . The following is a simple example:

In the background program or script, when the static resources are updated, issue an HTTP request to refresh the cache:

import requests

def refresh_cache():
  url = "http://your-nginx-server/purge-url"
  headers = {"Host": "your-hostname"}
  response = requests.get(url, headers=headers)
  
  if response.status_code == 200:
    print("Cache refreshed successfully!")
  else:
    print("Failed to refresh cache.")

In the Nginx configuration file, Configure address routing and cache refresh:

location ~ /purge-url {
  internal;
  proxy_cache_purge cache_zone "$scheme$request_method$host$request_uri$is_args$args";
}

Description:

  • location: Configure the route for processing cache refresh requests.
  • internal: Indicates that the route can only be accessed by internal requests and cannot be accessed by external requests.
  • proxy_cache_purge: Configure the cache area to be refreshed and related parameters.

With the above configuration, when issuing a GET request with X-Purge-Cache: 1 to http://your-nginx-server/purge-url , Nginx will refresh the corresponding cache according to the requested URI.

  1. Verification:
    To verify the correctness of the configuration, you can use a browser, Postman, or similar tool to issue a GET with X-Purge-Cache: 1 ask. If the cache is refreshed successfully, when accessing the corresponding static resource again, the latest version will be obtained from the backend server and the cache will be updated to ensure that the user sees the latest static resource.

Conclusion:
Through Nginx's reverse proxy cache configuration and real-time refresh of static resources, we can improve the performance and access speed of the website, and ensure that users can obtain the latest static resources in real time resource. This is especially important for websites that are frequently updated. It can also reduce the pressure on the back-end server and improve the stability and reliability of the website. I hope this introduction will be helpful to you!

The above is the detailed content of Nginx reverse proxy cache refresh configuration and update website static resources in real time. 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 Unit's Purpose: Running Web ApplicationsNGINX Unit's Purpose: Running Web ApplicationsApr 30, 2025 am 12:06 AM

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

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.

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.