gpwr
gpwr

Reputation: 1025

Reset Nginx static content caching

I have the following inside the server block of the nginx configuration:

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}

This seems great for files that almost never change but when they do change, what do you do to indicate to the clients loading these static files that something has changed? My initial thoughts are to change 'expires 365d' to 'expires 1d' but there must be a better way to do this?

Upvotes: 0

Views: 573

Answers (1)

Timo Stark
Timo Stark

Reputation: 3071

The Cache-Control header set by the expires directive will tell the Browser how and when the file will be request from the server again.

It's the recommended way of dooing so. All good! No direct need to change it to "just" 1d. I control the caching behaviour of the browser with the URL to the resource. Let me make an example:

A css or image file on the server. main.css and logo.jpg. In the HTML do something like <link rel="stylesheet" href="main.css?version=1" .......>. After changing the css file increase the version-number or simply change it to something else.

<link rel="stylesheet" href="main.css?version="2" .......>

Your Browser: "Hey that file got changed - I'll go get it!".

I have two different methods of "version numbering":

  • 1,2,3.....
  • git commit hash of a file.

What ever is good for you.

Upvotes: 1

Related Questions