Nginx proxy cache update configuration, real-time response to website content changes
Introduction:
With the continuous increase in website visits, how to improve the performance of the website has become an important issue. Nginx is a high-performance HTTP server and reverse proxy server, and proxy caching is an important part of it. In daily operation and maintenance, it is often necessary to update and modify the content of the website while maintaining the response speed when users access it. This article will introduce how to configure proxy caching in Nginx and enable it to respond to changes in website content in real time.
Configure Nginx proxy cache
In the Nginx configuration file, we need to add the following configuration to enable proxy cache:
http { proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m; server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; proxy_cache my_cache; proxy_cache_key $scheme$host$request_uri; proxy_cache_valid 200 304 12h; proxy_cache_use_stale updating; proxy_ignore_headers Cache-Control; } } }
In the above configuration, proxy_cache_path
Specifies the storage path and related parameters of the cache file. levels=1:2
represents the depth of the cache path, keys_zone
is the cache name and size limit, max_size
is the maximum size of the cache, inactive
is the cache inactivity time.
In the location part of the server segment, proxy_pass
specifies the address of the back-end service, proxy_cache
specifies the cache name used, proxy_cache_key
defines the cache key value, proxy_cache_valid
sets the validity period of requests with response codes 200 and 304, proxy_cache_use_stale
specifies whether to use the old cache when updating the cache For cached content, proxy_ignore_headers
sets ignored HTTP headers.
Use Nginx's proxy_cache_bypass directive to update the cache in real time
Nginx provides the proxy_cache_bypass
directive, which can be used to update the cache in real time. We can trigger cache updates by setting the corresponding HTTP header when the backend service responds. Here is an example:
import requests def update_cache(url): headers = { 'X-Cache-Bypass': '1', } response = requests.get(url, headers=headers) return response.text
In the above example code, by setting the X-Cache-Bypass
header to 1, we can tell Nginx to bypass the cache in the proxy cache, thus getting real-time Latest content.
Automatically trigger cache updates
In addition to manually triggering cache updates, we can also automatically trigger cache updates by using scheduled tasks or Webhooks. The following is a sample code using Python's web framework Flask and Celery:
from flask import Flask, request from celery import Celery app = Flask(__name__) celery = Celery(app.name, broker='redis://localhost:6379/0') @app.route('/update_cache', methods=['POST']) def update_cache(): url = request.form.get('url') result = celery.send_task('tasks.update_cache', args=[url]) return 'Task submitted' @celery.task def update_cache(url): headers = { 'X-Cache-Bypass': '1', } response = requests.get(url, headers=headers) return response.text if __name__ == '__main__': app.run()
In the above example, we used Flask to create a simple interface /update_cache
to trigger caching through POST requests of updates. After receiving the request, we use Celery to asynchronously perform the cache update task and return the corresponding results.
Conclusion:
Through the above configuration and sample code, we can configure proxy caching in Nginx and respond to changes in website content in real time. This improves the performance of the site while enabling quick updates and modifications to the site content.
Of course, in actual applications, factors such as cache invalidation strategy, high availability, and security may also need to be considered. During detailed configuration, adjustments need to be made according to actual needs. I hope this article will be helpful to learn and understand Nginx proxy cache update configuration.
The above is the detailed content of Nginx proxy cache update configuration to respond to changes in website content in real time. For more information, please follow other related articles on the PHP Chinese website!