Forum: NGINX how to add Cache-Control: public to static files

14eddebaded16496d1198b2e43d44946?d=identicon&s=25 Jauder Ho (jauderho)
on 2009-10-06 04:17

Hi, I have nginx set up to serve static content and to set it to maximum
expires currently. However, I would also like to set the Cache-Control
flag to public so that proxies will know that this is okay to cache.
However, when I try this using the add_header directive, this adds an
additional Cache-Control: header instead of modifying the existing one.

nginx config fragment:
    location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
        access_log off;
        expires max;
        add_header Cache-Control public;
    }


what I get using curl:
HTTP/1.1 200 OK
Date: Tue, 06 Oct 2009 02:12:27 GMT
Content-Type: image/gif
Content-Length: 96
Last-Modified: Thu, 17 Sep 2009 01:51:06 GMT
Connection: close
Vary: Accept-Encoding
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
Cache-Control: public
Accept-Ranges: bytes
B0c1cf80d56f0207dd8cf0c4651fb223?d=identicon&s=25 Chris Zimmerman (Guest)
on 2009-10-06 04:45
(Received via mailing list)
add_header isn't used for rewriting the header. Currently nginx supports
only "no-cache" and "max-age=###" values
in "Cache-Control.
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2009-10-06 09:13
(Received via mailing list)
Hello!

On Tue, Oct 06, 2009 at 04:17:20AM +0200, Jauder Ho wrote:

>         access_log off;
> Last-Modified: Thu, 17 Sep 2009 01:51:06 GMT
> Connection: close
> Vary: Accept-Encoding
> Expires: Thu, 31 Dec 2037 23:55:55 GMT
> Cache-Control: max-age=315360000
> Cache-Control: public
> Accept-Ranges: bytes

Multiple Cache-Control lines are perfectly valid per RFC2616, it
shouldn't cause any problems.

If you really want them to be in single line, just set everything
in add_header instead of using "expires max" to set
Cache-Control: max-age and Expires.  E.g.

     add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
     add_header Cache-Control "public, max-age=315360000";

Maxim Dounin
5640e332954fc0006aea97a155ce0afd?d=identicon&s=25 Igor Sysoev (Guest)
on 2009-10-06 10:11
(Received via mailing list)
On Mon, Oct 05, 2009 at 10:35:13PM -0400, Chris Zimmerman wrote:

> add_header isn't used for rewriting the header. Currently nginx supports
> only "no-cache" and "max-age=###" values
> in "Cache-Control.

These values are affected by "expires" directive only.
You may add any Cache-Control values via add_header.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.