Skip to content
How to Disable Browser Caching in Nginx: Two Effective Methods
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

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";
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendOperationsnginxHTTP headersCache Controlweb-configuration
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.