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

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

王林
王林 Original
2023-07-07 08:06:09 1609browse

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.

  1. 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_pathSpecifies the storage path and related parameters of the cache file.levels=1:2represents the depth of the cache path,keys_zoneis the cache name and size limit,max_sizeis the maximum size of the cache,inactiveis the cache inactivity time.

In the location part of the server segment,proxy_passspecifies the address of the back-end service,proxy_cachespecifies the cache name used,proxy_cache_keydefines the cache key value,proxy_cache_validsets the validity period of requests with response codes 200 and 304,proxy_cache_use_stalespecifies whether to use the old cache when updating the cache For cached content,proxy_ignore_headerssets ignored HTTP headers.

  1. Use Nginx's proxy_cache_bypass directive to update the cache in real time
    Nginx provides theproxy_cache_bypassdirective, 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 theX-Cache-Bypassheader to 1, we can tell Nginx to bypass the cache in the proxy cache, thus getting real-time Latest content.

  2. 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_cacheto 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!

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