GrowSing
GrowSing

Reputation: 623

NGINX, Disable cache in specific folder for a specific file type

I have -unfortunately Windows- Nginx server that I use for a static content (like product photos and so on). Currently I have had a global setting for caching, but now I need to change it little.

I have a folder which path looks something like this:

E:\xampp\srv\project-files\projectX\files\users\user-hash\visualisator\views

As you can see in the path is the user-hash variable which changes. And in this folder I have *.jpg files that need to have cache disabled.

I have already tried something like this (located on top of the other (global) location settings):

location ~ /users/ {
   alias "E:/xampp/srv/project-files/projectX/files/users";
   expires -1;
   add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
 }

And I have been hoping that it will at least disable cache for all the files in this folder and further. But the only result I am getting from this is http 403 .

I can live with disabled cache from the folder users and further if it will works, but the best solution would be to disable cache for the whole path (with user-hash variable included) and just for a specific file type (*.jpg).

Any idea or recommendation how to achieve this? PS: NGinx is new for me, I have spent like 8 hours tops with this technology, so sorry if it is stupid question, but I can't possibly figure it out or find it anywhere.

Thank you in advance!

Upvotes: 13

Views: 23194

Answers (2)

Luis H Cabrejo
Luis H Cabrejo

Reputation: 316

This worked for me, the file that I needed to exclude is a PHP file. Maybe you can use the same code for your location ~ .*files/projectX/files/users/.*jpg$ it may work too. I tried all samples above, and in other pages. The only thing worked for me was this. My file was a PHP and a I had to create a complete copy of my last part of the configuration file. Maybe replacing your location will work for you.

location ~ player\.php {
        set $no_cache 1;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;   
            if (-f $request_filename) {
                # Only throw it at PHP-FPM if the file exists (prevents some PHP exploits)
                fastcgi_pass   unix:/tmp/run/php7-fpm.sock;
                #fastcgi_read_timeout 3600;
            }         
}

Upvotes: 0

GrowSing
GrowSing

Reputation: 623

location ~ .*files/projectX/files/users/.*jpg$ {
          expires -1;
          add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        }

This does the trick.

Upvotes: 25

Related Questions