Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX proxy_pass not caching content

Tags:

caching

nginx

I'm having issues getting NGINX to cache thumbnails that I'm pulling from Dropbox using the proxy_pass command. On the same server that NGINX is running I run the following command multiple times

 wget --server-response --spider  http://localhost:8181/1/thumbnails/auto/test.jpg?access_token=123

and get the exact same response with X-Cache: MISS every time

HTTP/1.1 200 OK Server: nginx/1.1.19 Date: Wed, 25 Mar 2015 20:05:36 GMT Content-Type: image/jpeg Content-Length: 1691 Connection: keep-alive pragma: no-cache cache-control: no-cache X-Robots-Tag: noindex, nofollow, noimageindex X-Cache: MISS

Here's my meat of my nginx.conf file .. any ideas on what I'm doing wrong here?

## Proxy Server Caching
proxy_cache_path  /data/nginx/cache  keys_zone=STATIC:10m max_size=1g;


## Proxy Server Setting
server {
    listen *:8181;

    proxy_cache     STATIC;
    proxy_cache_key "$request_uri";
    proxy_cache_use_stale  error timeout invalid_header updating
                   http_500 http_502 http_503 http_504;

    location ~ ^/(.*) {
    set $dropbox_api 'api-content.dropbox.com';
    set $url    '$1';

    resolver 8.8.8.8;   

    proxy_set_header    Host    $dropbox_api;

    proxy_cache     STATIC;
    proxy_cache_key     "$request_uri";
    proxy_cache_use_stale   error timeout invalid_header updating
                   http_500 http_502 http_503 http_504;

    add_header X-Cache $upstream_cache_status; 

    proxy_pass https://$dropbox_api/$url$is_args$args;
    }

    ##Error Handling
    error_page 500 502 503 504 404 /error/;  
    location = /error/ {  
    default_type text/html;
    }   
}
like image 258
Chad Brown Avatar asked Dec 20 '22 05:12

Chad Brown


1 Answers

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;
    }
}
like image 133
Chad Brown Avatar answered Jan 09 '23 16:01

Chad Brown