curl -X GET -I -H "Cache-Control: no-cache" does not bypass

7.3k views Asked by At

I am doing the following

$ curl -X GET -I -H "Cache-Control: no-cache" https:/myserver/myimage.jpg
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 24 Jun 2015 22:55:31 GMT
Content-Type: image/jpeg
Content-Length: 37502
Connection: keep-alive
Etag: "wzsdm-1434973626-37502-1204291434"
Cache-Control: max-age=604800, public
Expires: Wed, 01 Jul 2015 22:43:01 GMT
X-Proxy-Cache: HIT

Where I expect to get the following instead

X-Proxy-Cache: BYPASS

My configuration on Nginx is as follow:

   location ~* \.(?:jpg|jpeg|gif|png|pdf|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ { 
      proxy_cache my_zone;
      add_header X-Proxy-Cache $upstream_cache_status;

      include proxy_params;
      proxy_pass http://my-backend;
   }

Is there any configuration I could have missed out?

1

There are 1 answers

1
ilumos On

By default, Nginx's cache does not honour the Cache-Control:no-cache request header, nor the Pragma:no-cache request header. You must explicitly configure Nginx to bypass the cache and pass the request onto the origin server when the user agent sends these request headers.

Add the following lines to your configuration file:

proxy_cache_bypass $http_pragma;
proxy_cache_bypass $http_cache_control;

From Nginx's caching guide:

How Does NGINX Handle the Pragma Header?

The Pragma:no‑cache header is added by clients to bypass all intermediary caches and go straight to the origin server for the requested content. NGINX does not honor the Pragma header by default, but you can configure the feature with the following proxy_cache_bypass directive: