Skip to main content
Caching helps you serve repeated requests faster by storing frequently accessed content closer to the client. Think of it like ordering the same coffee and cookie every morning: at first, the barista prepares your order on demand, but over time they have it ready before you even ask. Similarly, Nginx can remember—and quickly deliver—static or dynamic resources without hitting your backend every time.
The image illustrates a server caching system, showing how a server stores and provides quick access to information like images, video files, style sheets, and HTML pages.
When a user revisits a website, cached assets are served directly, reducing backend load and improving response times.
The image illustrates a process where a user revisits a website, and the saved version is retrieved from a server, resulting in time savings.
Nginx can act as both a reverse proxy and a cache server. It sits in front of your application, intercepting requests and serving cached responses whenever possible.
The image illustrates an Nginx caching server setup, showing a backend server, a cache server with various file types, and a user accessing content on a laptop.

Core Proxy Cache Configuration

Below is a minimal nginx.conf snippet illustrating the main caching directives:

proxy_cache_path

Defines where and how cache files are stored:
Verify that the Nginx user has read/write access to your cache directory (/var/lib/nginx/cache by default).

proxy_cache_key

By default, Nginx uses the full request URL as the cache key. You can customize it to include protocol, HTTP method, host, and URI for better uniqueness:
The image illustrates the process of handling a request in NGINX using a proxy cache key to access cached data, including HTML, JavaScript, CSS, and GIF files.

proxy_cache_valid

Control how long different response codes are kept in cache:

Optional: Bypassing the Cache

For certain dynamic routes or when clients send cache-control headers, you may want to skip cache lookup:
Overusing proxy_cache_bypass will reduce cache effectiveness and increase load on your backend. Use it only when necessary.

Verifying Cache Behavior

Inspect the X-Proxy-Cache header in your HTTP response to determine whether content was served from cache:
Example response headers:
The image shows a list of HTTP response headers with various details such as content type, cache control, and date. It is titled "Confirmation" and includes a copyright notice from KodeKloud.
Use browser developer tools or your server logs to monitor cache hits and misses. A HIT indicates a cached response, while a MISS shows that Nginx forwarded the request upstream.

Further Reading

Watch Video