0
0
Nginxdevops~5 mins

Cache validity rules in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes web content changes often, but browsers or servers keep old copies. Cache validity rules tell nginx when to keep or refresh cached content to serve fresh data without slowing down your site.
When you want to speed up your website by serving cached pages but need to update them after a certain time.
When you want to control how long images or scripts are stored in cache before checking for updates.
When you want to avoid serving outdated content to users by setting rules for cache expiration.
When you want to reduce load on your backend servers by serving cached responses for repeated requests.
When you want to customize cache behavior based on HTTP status codes or response headers.
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;
            proxy_cache_valid any 5m;
        }
    }
}

proxy_cache_path defines where cache files are stored and cache zone settings.

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

proxy_cache_valid sets how long responses with specific HTTP status codes are considered valid in cache before refreshing.

Commands
Check the nginx configuration file syntax to ensure there are no errors before reloading.
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 cache validity rules without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to check response headers and verify if caching headers are present.
Terminal
curl -I http://localhost/
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 05 Jun 2024 12:00:00 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Wed, 05 Jun 2024 11:50:00 GMT X-Cache-Status: HIT
Key Concept

If you remember nothing else from this pattern, remember: cache validity rules tell nginx how long to keep cached responses before fetching fresh content.

Common Mistakes
Setting proxy_cache_valid without specifying status codes or using incorrect syntax.
Nginx may ignore the rule or cache content indefinitely, causing stale content to be served.
Always specify valid HTTP status codes and durations in the format: proxy_cache_valid 200 10m;
Not testing nginx configuration before reload.
A syntax error can cause nginx to fail to reload, leading to downtime.
Run 'sudo nginx -t' to verify configuration syntax before reloading.
Reloading nginx without proper permissions.
The reload command will fail silently or show permission denied errors.
Use sudo or run as root to reload nginx.
Summary
Define cache storage and zone with proxy_cache_path in nginx.conf.
Use proxy_cache_valid to set how long cached responses stay valid based on HTTP status codes.
Test nginx configuration syntax with 'nginx -t' before reloading to apply changes safely.