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.
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.
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.
#!/bin/bash curl -X PURGE http://localhost/page1 curl -X PURGE http://localhost/page2 curl -X PURGE http://localhost/page3
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.
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!