Nginx · December 18, 2023

Nginx Command: proxy_cache_valid

Nginx Command: proxy_cache_valid

Nginx is a popular web server and reverse proxy server that is known for its high performance, scalability, and flexibility. One of the key features of Nginx is its ability to cache content, which can significantly improve the performance of websites and web applications. In this article, we will explore the Nginx command proxy_cache_valid and how it can be used to control the caching behavior of Nginx.

Understanding Caching in Nginx

Caching is the process of storing copies of frequently accessed data in a cache, which allows subsequent requests for that data to be served faster. In the context of web servers, caching can be used to store static content such as images, CSS files, and JavaScript files, as well as dynamic content that is generated by the server.

Nginx uses a two-level caching mechanism: the proxy cache and the fastcgi cache. The proxy cache is used for caching content from upstream servers, while the fastcgi cache is used for caching content generated by FastCGI processes.

The proxy_cache_valid Command

The proxy_cache_valid command is used to control the caching behavior of Nginx for responses from upstream servers. It specifies the maximum time that a response can be considered valid and served from the cache without revalidating it with the upstream server.

The syntax of the proxy_cache_valid command is as follows:

proxy_cache_valid <status> <time>;

Here, <status> is the HTTP status code for which the caching behavior is being defined, and <time> is the maximum time that a response with the specified status code can be considered valid.

For example, to specify that responses with a 200 (OK) status code can be cached for 1 hour, you can use the following command:

proxy_cache_valid 200 1h;

You can also use the special value any to specify that responses with any status code can be cached. For example:

proxy_cache_valid any 10m;

This command specifies that any response can be cached for 10 minutes.

Controlling Cache Invalidation

In addition to specifying the maximum time that a response can be considered valid, the proxy_cache_valid command also controls cache invalidation. When a response is invalidated, it means that it is no longer considered valid and will not be served from the cache.

You can use the proxy_cache_valid command to specify the conditions under which a response should be invalidated. For example, you can use the proxy_cache_valid command with a status code of 404 (Not Found) and a time of 0 to specify that responses with a 404 status code should never be cached:

proxy_cache_valid 404 0;

This ensures that if a resource is not found, subsequent requests for that resource will always be forwarded to the upstream server, rather than being served from the cache.

Conclusion

The proxy_cache_valid command is a powerful tool for controlling the caching behavior of Nginx. By specifying the maximum time that a response can be considered valid and controlling cache invalidation, you can optimize the performance of your website or web application.

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