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.
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";
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
