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

Nginx 为statics文件夹中的特定子文件夹设置缓存控制标头,nginx,nginx-location,nginx-config,Nginx,Nginx Location,Nginx Config,我有一个具有此结构的静态文件夹 └── static ├── images ├── locales └── robots.txt 我已为此文件夹设置了location指令以公开缓存文件 location /static { access_log off; log_not_found off; expires 1y; autoindex off; add_header

我有一个具有此结构的
静态
文件夹

└── static
    ├── images
    ├── locales
    └── robots.txt
我已为此文件夹设置了
location
指令以公开缓存文件

location /static {
    access_log        off;
    log_not_found     off;
    expires           1y;
    autoindex         off;
    add_header        Cache-Control "public";
}
我想将
json
文件夹中的所有
locales文件的
Cache Control
头更改为
public,当revalidate=60时变为stale,如果error=60时变为stale

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

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

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

这管用

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