Operations 2 min read

How to Disable Browser Caching in Nginx: Two Effective Methods

Learn two practical Nginx configurations to prevent browsers from caching HTML files by setting appropriate Cache-Control, Pragma, and Expires headers, with examples for both standard location blocks and conditional directives, ensuring fresh content delivery for your web applications.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
How to Disable Browser Caching in Nginx: Two Effective Methods

Method 1:

location / {
    index  index.html index.htm;
    add_header Cache-Control no-cache,no-store;
    try_files $uri $uri/ /index.html;
    include mime.types;
    if ($request_filename ~* .*\.(htm|html)$) {
        add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
    }
}

Method 2:

location / {
    index  index.html index.htm;
    add_header Cache-Control no-cache,no-store;
    try_files $uri $uri/ /index.html;
    include mime.types;
    #if ($request_filename ~* .*\.(htm|html)$) {
    #   add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
    #}
}

Additional specific block to handle the index file directly:

location = /index.html {
    #add_header Cache-Control "no-cache, no-store, must-revalidate";
    add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
    add_header Pragma "no-cache";
    add_header Expires "0";
}
backendoperationsNginxhttp-headersCache-Controlweb-configuration
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.