Nginx 缓存非活动参数不起作用

问题描述

我想提供存储在不同服务器上的静态文件。为此,我使用具有以下配置的 Nginx 缓存。

Nginx 缓存配置:

proxy_cache_path   /var/cache/cdn levels=1:2 keys_zone=cdn:64m max_size=20g inactive=10m use_temp_path=off;

服务配置:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name <server-name>;

    ssl_certificate <fullchain>;
    ssl_certificate_key <key>;

    location / {
        # Activate caching
        proxy_cache cdn;

        # Cache becomes stale after 1 minute
        proxy_cache_valid 1m;

        # Download stale data only if it has been modified on origin 
        proxy_cache_revalidate on;

        # Use stale file origin is unaccessible 
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

        # If multiple clients get a MISS for the same file,# the file is downloaded form Origin only once
        proxy_cache_lock on;

        # Filesystem key
        proxy_cache_key $uri$is_args$args;          

        # Gives the status of the file returned
        # MISS,BYPASS,EXPIRED,STALE,UPDATING,REVALIDATED,HIT
        add_header X-Cache-Status $upstream_cache_status;

        # Origin server address
        proxy_pass <server origin>;
    }
}

server{
    listen 80;
    listen [::]:80;
    server_name <server-name>;

    if ($host = <server-name>) {
        return 301 https://$host$request_uri;
    }
}

此设置在全局范围内工作正常:文件已交付,重新验证有效,proxy_cache_valid 也有效...但是,inactive 中的 proxy_cache_path 参数似乎根本不起作用:10 之后分钟没有任何人请求文件,该文件不会从缓存中删除,仍然可以在 /var/cache/cdn 目录中看到。

这正常吗?我的配置搞砸了吗?

感谢您的帮助,

AlberichVR

解决方法

在网上大量研究和反复试验后,我发现了一个问题:我使用的缓存文件夹的权限为 700。将它们更改为 777 后,问题自行解决。