如何禁用特定路由的 Nginx 缓存?

如何禁用特定路由的 Nginx 缓存?

我想禁用特定目录的缓存:/sync/download

我的环境详细信息:

  • 操作系统 Linux (rocky)
  • 服务器:nginx/1.20.1

我将此代码块添加到我的 nginx 站点配置中:

location ~ .*sync/download/.*$ {
                        expires -1;
                        proxy_no_cache true;
                        add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
                        proxy_pass http://backend;
        }

我的完整 nginx 配置:

server {
        listen   443 ssl http2;
        listen       [::]:443 ssl http2;
        server_name ws;

        root         /usr/share/nginx/html;


        ssl_certificate "/etc/nginx/cert/cert1.pem";
        ssl_certificate_key "/etc/nginx/cert/cert1.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        access_log  /var/log/nginx/ws.access.log main;
        error_log  /var/log/nginx/ws.error.log;




        location / {
               proxy_pass http://backend;
               proxy_set_header Host $http_host;
               proxy_set_header Request-Id $request_id;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-Proto https;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_cache            BPH;

                location ~ .*sync/download/.*$ {
                        expires -1;
                        proxy_no_cache true;
                        add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
                        proxy_pass http://backend;
        }


        }


        if ($scheme = http) {
            return 301 https://$server_name$request_uri;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

}             

当我删除一个文件时,它仍然给出 200 的结果(图像已被删除)

如何禁用特定路径的缓存?

相关内容