NGINX not caching images or sending added headers

cacheconfigurationnginx

add_header directive and the proxy directive seems to be ignored. I am using nginx as a cdn to serve images and I would like it to cache the images. The below is my sites available for the cdn. The images are served fine but I don't see the X-Cache-Status in the headers nor does it seem to be populating the cache path with any content.

What am I missing?

nginx version: nginx/1.10.0 (Ubuntu)

proxy_cache_path /var/www/html/nginx-cache levels=1:2 keys_zone=cdn:100m max_size=25g inactive=60m use_temp_path=off;

# Expires map
map $sent_http_content_type $expires {
    default                    off;
    text/html                  epoch;
    text/css                   max;
    application/javascript     max;
    ~image/                    max;
}


server {
  listen 80;
  server_name applebeescdn;

  # Proxy Cache
  proxy_cache cdn;
  proxy_cache_key "$host$request_uri $cookie_user";
  proxy_cache_min_uses 1;
  proxy_cache_valid 200 302 120m;
  proxy_cache_valid 404 1m;
  proxy_ignore_headers "Set-Cookie";
  proxy_hide_header "Set-Cookie";
  proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
  proxy_buffering on;

  location / {
    expires $expires;
    root /var/www/html/;
    add_header 'X-Cache-Status' "$upstream_cache_status" always;
  }

}

Best Answer

I think you are misunderstanding how to use proxy_cache. You have to have a proxy_pass if you are using proxy_cache (i.e., a separate origin server for which this nginx instance is acting as a reverse proxy). You can read more about how to set up an origin server here.