NGINX,禁用特定文件类型的特定文件夹中的缓存

13

很遗憾,我使用的是Windows系统的Nginx服务器,用于存放静态内容(例如产品照片等)。目前我已经设置了全局缓存,但现在需要对其进行一些更改。

我有一个文件夹,其路径大致如下:

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

从路径中可以看出user-hash变量是会变化的。在这个文件夹中,我有一些*.jpg文件需要禁用缓存。

我已经尝试过类似下面这样的内容(位于其他全局位置设置的顶部):

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';
 }
我一直希望它至少可以禁用此文件夹及其子文件夹中所有文件的缓存。但我得到的唯一结果是 http 403
如果可以在users文件夹及其后面禁用缓存,我也能接受,但最好的解决方案是禁用整个路径(包括user-hash变量),并仅对特定文件类型(*.jpg)禁用缓存。
有任何想法或建议如何实现吗? PS:NGinx对我来说很新,我只花了不到8个小时的时间接触这项技术,所以如果这是一个愚蠢的问题,我很抱歉,但我无法弄清楚或找到答案。
提前感谢!
2个回答

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

这就是窍门。


0

这对我有用,我需要排除的文件是一个PHP文件。也许你可以使用同样的代码来处理你的 location ~ .*files/projectX/files/users/.*jpg$,它可能也会起作用。我尝试了上面所有的示例,在其他页面也试过了。唯一有效的方法是这个。我的文件是一个PHP文件,我不得不创建完整的配置文件最后一部分的副本。也许替换你的location对你有用。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接