0
0
Nginxdevops~5 mins

Why caching improves response times in Nginx - Why It Works

Choose your learning style9 modes available
Introduction
Caching stores copies of web content so that future requests can be served faster. It reduces the time needed to get data from the original source by delivering saved content quickly.
When your website has many visitors requesting the same images or pages repeatedly.
When you want to reduce the load on your web server by serving stored content.
When you want to speed up response times for users accessing your site from different locations.
When your content changes infrequently and can be safely reused for some time.
When you want to improve user experience by delivering pages faster.
Config File - nginx.conf
nginx.conf
http {
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=60m use_temp_path=off;

    server {
        listen 80;

        location / {
            proxy_pass http://backend_server;
            proxy_cache my_cache;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            add_header X-Cache-Status $upstream_cache_status;
        }
    }
}

proxy_cache_path defines where cached files are stored and cache settings.

proxy_cache enables caching for the location using the defined cache zone.

proxy_cache_valid sets how long responses with certain status codes are cached.

add_header adds a header to show if the response was served from cache.

Commands
Check the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new caching configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Make a request to the server to see if the response is served from cache by checking the X-Cache-Status header.
Terminal
curl -I http://localhost/
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx X-Cache-Status: MISS Content-Type: text/html Content-Length: 612
Make the same request again to verify the response is now served from cache, improving response time.
Terminal
curl -I http://localhost/
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx X-Cache-Status: HIT Content-Type: text/html Content-Length: 612
Key Concept

If you remember nothing else from this pattern, remember: caching saves copies of content so future requests are served faster without asking the original server again.

Common Mistakes
Not testing nginx configuration before reload
This can cause nginx to fail to reload if there are syntax errors, leading to downtime.
Always run 'nginx -t' to check configuration syntax before reloading.
Not setting cache duration with proxy_cache_valid
Without cache duration, nginx may not cache responses or cache them indefinitely, causing stale content or no speed improvement.
Set appropriate cache durations for different response codes using proxy_cache_valid.
Forgetting to reload nginx after config changes
Changes won't take effect until nginx reloads, so caching won't work as expected.
Run 'systemctl reload nginx' after editing configuration.
Summary
Define a cache zone and path in nginx.conf to store cached content.
Enable caching in the server location and set cache durations for responses.
Test nginx configuration syntax before reloading to avoid errors.
Reload nginx to apply caching settings without downtime.
Verify caching works by checking response headers for cache status.