Nginx · December 18, 2023

Nginx Command: proxy_cache_use_stale

Nginx Command: proxy_cache_use_stale

Nginx is a popular web server and reverse proxy server that is known for its high performance, scalability, and flexibility. It is widely used by many websites and web applications to handle a large number of concurrent connections efficiently. One of the key features of Nginx is its caching mechanism, which can significantly improve the performance of websites by serving cached content instead of generating it dynamically for each request.

When it comes to caching, Nginx provides various directives and commands to control the behavior of the cache. One such command is proxy_cache_use_stale, which allows you to specify how Nginx should handle stale content in the cache.

Understanding Stale Content

In the context of Nginx caching, stale content refers to the cached content that has expired or become invalid due to changes on the origin server. By default, when a request is made for a resource that is already cached but has become stale, Nginx will fetch the updated content from the origin server and serve it to the client. However, this process can introduce additional latency and put unnecessary load on the origin server.

Using proxy_cache_use_stale

The proxy_cache_use_stale command allows you to control how Nginx handles stale content in the cache. It accepts several parameters that define the conditions under which Nginx should serve stale content instead of fetching it from the origin server.

Here are the available parameters:

  • error: Serves stale content if there is an error while fetching the updated content from the origin server.
  • timeout: Serves stale content if the origin server takes too long to respond.
  • invalid_header: Serves stale content if the response from the origin server has invalid headers.
  • updating: Serves stale content if the updated content is being fetched from the origin server.
  • http_500: Serves stale content if the origin server returns a 500 Internal Server Error.
  • http_502: Serves stale content if the origin server returns a 502 Bad Gateway.
  • http_503: Serves stale content if the origin server returns a 503 Service Unavailable.
  • http_504: Serves stale content if the origin server returns a 504 Gateway Timeout.
  • http_403: Serves stale content if the origin server returns a 403 Forbidden.
  • http_404: Serves stale content if the origin server returns a 404 Not Found.
  • http_429: Serves stale content if the origin server returns a 429 Too Many Requests.

You can use one or more of these parameters in combination to define the desired behavior. For example, if you want Nginx to serve stale content only when there is an error or a timeout while fetching the updated content, you can use the following configuration:

proxy_cache_use_stale error timeout;

This configuration tells Nginx to serve stale content if there is an error or a timeout, but not in other cases like invalid headers or while the content is being updated.

Conclusion

The proxy_cache_use_stale command in Nginx provides a powerful tool to control how stale content is handled in the cache. By using this command effectively, you can optimize the performance of your website or web application by reducing the load on the origin server and minimizing the latency for serving cached content.

For more information about Nginx and its caching capabilities, you can visit the Server.HK website.