为静态文件夹中的特定 sub-folder 设置 cache-control header
Set cache-control header for a specific sub-folder inside statics folder
我有一个具有这种结构的 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 文件夹中的所有 json
文件的 Cache-Control
header 更改为 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";
}
}
显然我真的很接近解决方案,我只需要在嵌套位置的正则表达式前面添加 ~
,
这有效!
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";
}
}
我有一个具有这种结构的 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 文件夹中的所有 json
文件的 Cache-Control
header 更改为 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";
}
}
显然我真的很接近解决方案,我只需要在嵌套位置的正则表达式前面添加 ~
,
这有效!
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";
}
}