为statics文件夹中的特定子文件夹设置缓存控制标头

时间:2019-10-29 15:36:42

标签: nginx nginx-location nginx-config

我有一个具有这种结构的static文件夹,

└── static
    ├── images
    ├── locales
    └── robots.txt

我为此文件夹设置了location指令,以公开地缓存文件。

location /static {
    access_log        off;
    log_not_found     off;
    expires           1y;
    autoindex         off;
    add_header        Cache-Control "public";
}

我想将 locales 文件夹内的所有Cache-Control文件的public, stale-while-revalidate=60, stale-if-error=60标头更改为json

我尝试过嵌套位置,但是没有成功,

location /static {
    access_log        off;
    log_not_found     off;
    expires           1y;
    autoindex         off;
    add_header        Cache-Control "public";

    location /static/locales/.*/.*\.json$ {
         expires      1w;
         add_header   Cache-Control "public,stale-while-revalidate=60, stale-if-error=60";
     }
}

1 个答案:

答案 0 :(得分:0)

显然,我真的很接近解决方案,我所需要的只是在嵌套位置的正则表达式前面添加~

这行得通!

location /static {
    access_log        off;
    log_not_found     off;
    expires           1y;
    autoindex         off;
    add_header        Cache-Control "public";

    location ~ /static/locales/.*/.*\.json$ {
         expires      1w;
         add_header   Cache-Control "public,stale-while-revalidate=60, stale-if-error=60";
     }
}