Nginx – Disable nginx caching for images in particular directory

cachenginx

There is a directory on my webserver which contains images that I don't want to be cached.

Nginx is frontend to Apache. I have caching enabled for static resources in nginx.conf:

server {
    listen 80;
        server_name www.mydomain.com mydomain.com;

        location / {
            root /home/somedomain/public_html/site;
            proxy_pass  http://backend;
            include /etc/nginx/proxy.conf;
        }

        location ~* \.(css|js)$ {
                  root /home/somedomain/public_html/site;
                  add_header  Last-Modified: $date_gmt;
                  expires 1y;
                  access_log off;
        }

        location ~* \.(jpg|jpeg|gif|png|ico|bmp|swf)$ {
                  root /home/somedomain/public_html/site;
                  expires max;
                  access_log off;
        }

        location ~ \.php { 
            proxy_pass  http://backend;
            include /etc/nginx/proxy.conf;
        }


}

I tried to add to the end:

location /home/somedomain/public_html/site/dontcache/ \.png {
                 root /home/somedomain/public_html/site/dontcache;
                 expires off;
}

Also tried expires -1; and expires 1m; but all of that doesnt seem to work.

I know it is simple but I just can't get why it doesnt work for me.

Best Answer

location ^~ /home/somedomain/public_html/site/dontcache/ {
  root /home/somedomain/public_html/site/dontcache;
  expires epoch;
}

The order of matching for location directives is described here