guy mograbi
guy mograbi

Reputation: 28598

What to do with nginx cache for HTML files?

I keep running into issues with caching for HTML files in my projects.

I have a cache bust mechanism for static resources (images, scripts, css etc..) but all these solutions seem to not handle HTML cache problem.

I added expires 0; to Nginx on all HTML files, but it seems to me I am missing an obvious solution.

location ~ / {
    if ( $document_uri ~* \.(html)$ ){
         expires 0;
    }
    root /var/www/my-website;

}

that looks so ugly(!) and besides, if is evil and I am certain all websites in the world need to handle this issue, so there's got to be a better way to resolve this.

In angular I tried using html2js - which basically turns all the HTML files to JavaScript and then it undergoes the same cache bust mechanism as the rest.

But that also forces users to download a big fat JavaScript file, which defeats in some way the use in angular - which loads the templates as needed, making it very light and fast.

How can I resolve HTML cache problem?

Upvotes: 0

Views: 1459

Answers (1)

Aleksey Deryagin
Aleksey Deryagin

Reputation: 2675

Try this 2 almost equal variants, place them after location ~ / {} section:

location ~ \.html$ {
  add_header Cache-Control "no-cache, no-store";
...
}

or

location ~ \.html$ {
  expires -1;
...
}

Upvotes: 2

Related Questions