7

I have a cache invalidation system in place, so I need to set the cache for css|js to never expire.

I tried this

location ~* \.(js|css)$ { # |png|jpg|jpeg|gif|ico
  expires max;
  #log_not_found off; # what's this for?
}

And this is what I see in firebug:

enter image description here

So as you can see, requests are still being made, though they only receive a 'not modified' response. But I want to avoid them entirely, is that possible?

Also, I've read that an expirires of more than 1 year is or will be considered invalid in the standard. Is that true?

Edit

Let me complete @DisgruntledGoat's answer:

  • Normal access to the url, like clicking on a link or in the url bar + enter: cache works, only 1 request issued for that url, and 0 for the assets.
  • F5 to reload: many requests issued, but you receive all '304 not modified' responses for the assets, so they are not downloaded
  • ctrl + F5 to reload without cache: many requests, all '200 success', all the assets are downloaded again.
1
  • 1
    I've suggested this topic be migrated to Server Fault where their users have much more experience running servers including Nginx, you'll get better help there.
    – Anagio
    Aug 19, 2012 at 5:08

2 Answers 2

6

Are you reloading the page to test this? When you do that, browsers generally request every file again (at least Chrome does). If you instead click links to different pages on your site, the browser should just use the cache and not re-request files.

Additionally, according to the spec the Expires directive can only have a date up one year in the future, so 2037 is an invalid value. Having said that, browsers generally accept far-future values just fine.

2
3

This cannot be done in another way you try it. It depends on the clients browser how cache is handled. Setting cache should not exceed one year more than the modification date Source.

Nginx is very effective, if you have < 1,000,000 visitors every day, you really don't have to worry about requests sent to Nginx. If it is about bandwidth, if a browser gets a 304 responce, it still uses the data from its cache, so no bandwidth is spilled.

1
  • From that link: once they're set and the resource is downloaded, the browser will not issue any GET requests for the resource until the expiry date or maximum age is reached. But my browser is issuing GET requests, for some reason. Though as you said, it's not using badwidth because of the 304 response. Aug 18, 2012 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .