In this lesson, you’ll compare Nginx acting as a reverse proxy without caching and with caching to see how offloading static assets can improve backend performance.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
1. No-Cache Reverse Proxy
When caching is disabled, every client request passes through Nginx directly to Apache. Assets like CSS, JavaScript, and images are fetched from the backend on each hit.
2. Caching Enabled
Withproxy_cache enabled, Nginx stores the first response and serves it on subsequent requests. The Apache servers only handle the initial fetch.

3. Baseline: Watching Apache Logs
First, observe raw traffic on your backend nodes:https://example.com in your browser. You’ll see repeated GETs:

Cache-Control headers are present.
4. Inflate Asset Sizes for Demo
To highlight caching benefits, increase each JPEG to about 20 MB on both nodes:Creating large files can consume significant disk space. Clean up after testing.
5. Configure Nginx Caching
5.1. Global Cache Settings
Edit/etc/nginx/nginx.conf and, inside the http { ... } block, add:
Ensure
/var/lib/nginx/cache exists and is writable by the Nginx user.| Directive | Description | Example |
|---|---|---|
| proxy_cache_path | Defines cache location, hierarchy, and zone | /var/lib/nginx/cache levels=1:2 keys_zone=app_cache:10m; |
| proxy_cache_key | Constructs a unique key for each request | "$scheme$request_method$host$request_uri" |
| proxy_cache_valid | Sets TTL for cached responses by status code | 200 302 10m; 404 1m; |
5.2. Server Block: Enable Cache
Open your SSL virtual host (/etc/nginx/sites-available/example-https) and under server { listen 443 ssl; ... }, update the location / block:
6. Log Cache Status in Apache
To captureHIT or MISS, add %{X-Proxy-Cache}i to your Apache log format in /etc/apache2/apache2.conf:
7. Verify Cache Directory and Hits
Initially, the cache directory is empty:MISS entries:

206 partial responses:
8. Browser Network View
Finally, use your browser’s Network panel to verify that large assets load instantly from cache:
Next Steps
This demo underscored how caching reduces backend load. For even better performance, consider enabling response compression withgzip or brotli next.