Home  >  Article  >  Operation and Maintenance  >  Nginx proxy cache update configuration to respond to website changes in real time

Nginx proxy cache update configuration to respond to website changes in real time

WBOY
WBOYOriginal
2023-07-04 08:54:092005browse

Nginx proxy cache update configuration, real-time response to website changes

Abstract: This article will introduce how to use Nginx proxy cache update configuration to achieve immediate response to updates when website content changes, improving website performance and user experience. At the same time, we will provide some practical code examples to help readers better understand and apply this feature.

  1. Introduction
    Nginx is a high-performance HTTP and reverse proxy server that is widely used in the deployment of Internet applications. In proxy mode, Nginx can cache the static content of the website, reduce the load on the source server, and speed up website access. However, when the website content changes, the default configuration of Nginx does not immediately update the cache, causing users to see the old page content. In order to solve this problem, we can achieve instant updates of the Nginx proxy cache through some tricks and configurations.
  2. Configuration file modification
    First, we need to modify the Nginx configuration file to ensure that the cache can be refreshed in real time when the website content is updated. We can achieve this through the following configuration items:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
proxy_cache_key "$request_method|$host|$request_uri";
proxy_cache_valid 200 301 302 10m;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;

Among them, proxy_cache_path specifies the storage path and size limit of the cache file; proxy_cache_key defines the cache The key name ensures that the cache can be refreshed every time the request URL changes; proxy_cache_valid is used to specify the cache validity period of HTTP response codes 200, 301, and 302; proxy_cache_use_stale is used in the source Allows expired caches to be used in case of server errors.

  1. Cache update rules
    By default, Nginx will actively go to the source server to request new content only after the cache expires. And we want to be able to update the cache immediately when the website content changes. In order to achieve this goal, you can set cache update rules through the following configuration items:
if ( $request_method = POST ) {
    add_header X-Nginx-Cache "BYPASS";
    proxy_cache_bypass $http_cache_control;
    proxy_no_cache 1;
}

The above configuration will capture the POST request and add X-Nginx-Cache## to the response header. #Field used to identify that the request needs to bypass the cache. Also, the proxy_cache_bypass and proxy_no_cache directives will ensure that this request will not be cached.

    Script scheduled running
  1. In order to implement regular cache updates, we can write a script to refresh the Nginx cache through scheduled tasks. The specific script content is as follows:
  2. #!/bin/bash
    
    curl -X PURGE http://localhost/page1
    curl -X PURGE http://localhost/page2
    curl -X PURGE http://localhost/page3
Notice that the

curl command is used in the above script to send a PURGE request to Nginx to clear the cache of a specific page. We can add the page URL that needs to refresh the cache to the script according to the actual situation. Then, use a scheduled task tool (such as cron) to run this script regularly to achieve scheduled updates of the cache.

    Conclusion
  1. This article introduces how to use Nginx proxy cache update configuration to achieve instant response when website content changes. We achieve this function by modifying the Nginx configuration file, setting the cache storage path and update rules, and writing a regularly run script. The use of this feature can effectively improve website performance and user experience, and reduce the pressure on the source server from user requests. I hope readers can better understand and apply Nginx's proxy cache update configuration through the introduction and sample code of this article.

The above is the detailed content of Nginx proxy cache update configuration to respond to website changes 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