Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx add_header and cache control

When you use the add_header directive in nginx, the header is added to the response coming from the origin server.

Say the origin server returns cache-control public, max-age=60. But in the nginx reverse proxy location you set something like:

add_header cache-control public, max-age=10

What does this do exactly? There are 2 different scenarios I can think of:

1) Nginx respects the cache-control header from the origin server and stores the content in its cache with an expiration of 60 secs. Then passes on the response with an overwritten header causing the client to store the resource in its cache with an expiration of 10s.

or..

2) Nginx overwrites the response headers first and then interprets them. It stores the resource with an expiration of 10 secs and passes the response to the client which also caches the it with an expiration of 10 secs.

like image 539
Thijs Koerselman Avatar asked Sep 25 '13 10:09

Thijs Koerselman


2 Answers

If you want to totally discard the original servers header you can add:

proxy_hide_header 'Cache-Control';
add_header 'Cache-Control' "public, max-age=10";

This will strip the original header and add your own.

like image 50
Jon Betts Avatar answered Oct 21 '22 03:10

Jon Betts


Nginx adds its header just before the origin server, so you will have:

cache-control: public, max-age=10
cache-control: public, max-age=60

and the origin header will replace the nginx header.

The solution? Use nginx v1.4.3 that has the module more_set_headers and more_clear_headers in order to replace or clear the headers from origin.

You can download the module from here.
Here how to download nginx 1.4.3 and how to install it.
Here how to use the directives.

like image 26
nlopez Avatar answered Oct 21 '22 03:10

nlopez