NGINX proxy_pass not caching content
Solution 1:
Turns out that thumbnail requests returned from Dropbox include the header
Cache-Control: no-cache
and Nginx will adhere to these headers unless they are explicitly ignored which can be done by simply using the following config line that will ignore any caching control.
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
We also had issues placing the "proxy_ignore_headers" option in different areas within the nginx.conf file. Finally after much playing around we got it to work by explicitly setting it in the "location" block. The full snippet of the config file can be found below
## Proxy Server Caching
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:50m inactive=2h max_size=2g;
## Proxy Server Setting
server {
listen *:8181;
location ~ ^/(.*) {
set $dropbox_api 'api-content.dropbox.com';
set $url '$1';
resolver 8.8.8.8;
proxy_set_header Host $dropbox_api;
proxy_hide_header x-dropbox-thumbcachehit;
proxy_hide_header x-dropbox-metadata;
proxy_hide_header x-server-response-time;
proxy_hide_header x-dropbox-request-id;
proxy_hide_header cache-control;
proxy_hide_header expires;
add_header cache-control "private";
add_header x-cache $upstream_cache_status; # HIT / MISS / BYPASS / EXPIRED
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
proxy_pass https://$dropbox_api/$url$is_args$args;
}
}
Solution 2:
In order to cache the proxy response the request between Nginx and origin should be cookie-less:
proxy_hide_header Set-Cookie;
proxy_ignore_headers Set-Cookie;
See full configuration with invalidation methods: https://gist.github.com/mikhailov/9639593