Chad Brown
Chad Brown

Reputation: 1667

NGINX proxy_pass not caching content

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;
    }   
}

Upvotes: 4

Views: 12944

Answers (3)

Zeal
Zeal

Reputation: 23

If above answers didn't solved your issue, try this:

proxy_cache_valid 200 2d; (or whatever amount of time and whatever response code you want)

add this where you are using or activating your proxy_cache <keys_zone_name>.

Apparently for me as soon as I remove proxy_cache_valid parameter caching status doesn't show up. Also documentation doesn't says that this is required field. Let me know if this works for you. So we might update documentation.

I expect expect proxy_cache get started page should show that you need 3 at least these parameters to get started: proxy_cache_path, proxy_cache and proxy_cache_valid

Upvotes: 1

Chad Brown
Chad Brown

Reputation: 1667

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;
    }
}

Upvotes: 10

Anatoly
Anatoly

Reputation: 15530

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

Upvotes: 2

Related Questions