Nginx – How to get Nginx to tell Firefox to cache the content

cachenginx

The following headers (from a static media response) don't lead to caching in Firefox. In Chrome they do.

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 22 Dec 2012 21:20:39 GMT
Content-Type: application/x-javascript; charset=utf-8
Last-Modified: Fri, 21 Dec 2012 19:28:54 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: public, max-age=86400
Content-Encoding: gzip

My Nginx server for static content looks like this:

server {
    listen 80;
    server_name static.example.com;

    # Logs
    access_log /var/log/nginx/static.example.com.access.log;
    error_log /var/log/nginx/static.example.com.error.log;

    # Root
    location / {
        alias /var/www/app/deps/current/repo/src/example/static/;
        add_header Cache-Control "public, max-age=86400";
    }
}

I've also tried using expires 24h;, in place of add_header ..., without luck.

I've found numerous, similar complaints on the Internet, but no solutions, nor any ideas of why this is, other than one person's mention of how Firefox purposely strays from the specification for handling HTTP 1.1 Cache-Control headers.

Is there any way to get Firefox to cache my static media, via a header or multiple headers? I don't want 75% of my server's static media requests to come from 20% of my users, just because Firefox is busted.

Note, I'm using Firefox 17.0.1 with default settings, but with Firebug installed and open to the Net tab.

Update:

Using, instead:

expires 1d;
add_header Cache-Control public;

Causes the headers to include:

Expires: Wed, 26 Dec 2012 19:54:20 GMT
Cache-Control: max-age=604800, public

This also doesn't cause Firefox to cache the content.

Best Answer

How do you determine that Firefox is not caching your files?

When a Last-Modified header is present in a reply, the browser is supposed to be making all subsequent requests for the same URL with a If-Modified-Since header, and then the response from a server may only contain a header (with no body) if the file has not been modified since.

IIRC, Gecko also looks at how old the Last-Modified date is. If it's relatively recent, then it only makes sense that the content changes all the time, and has to be re-requested (with a If-Modified-Since or the like) quite often. On the other hand, if the date is several weeks, months or years old, then the content is likely to be cached for an extended period of time.

Another thing to look at: do you actually have the correct timezone and time on your Firefox machine?