例如,我们在open_file_cache中启用了/etc/nginx/nginx.conf
open_file_cache max=10000 inactive=60s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
一些文件/my/file.jpg被更改了,我们知道它在服务器端。示例位置块,按下以下文件:
location ~ (.+)\.[^\.][^\.][^\.]?[^\.]?[^\.]?[^\.]?$
{
include /etc/nginx/upload.mime.types;
add_header Access-Control-Allow-Origin *;
root $hostdir;
try_files $uri =404;
error_page 404 /public/404.png;
}
如何清除缓存服务器端?
发布于 2022-12-02 10:00:45
您可以向文件请求添加特殊的参数识别。如果传递此参数,则需要设置选项open_file_cache_valid 0s
。示例:
location ~ (.+)\.[^\.][^\.][^\.]?[^\.]?[^\.]?[^\.]?$
{
include /etc/nginx/upload.mime.types;
error_page 419 = @purgecache;
add_header Access-Control-Allow-Origin *;
if ($arg_purgecache) {
#directly set "open_file_cache off;" here not works, don't know why
return 419;
}
root $hostdir;
try_files $uri =404;
error_page 404 /public/404.png;
}
location @purgecache
{
open_file_cache_valid 0s;
root $hostdir;
try_files $uri =404;
error_page 404 /public/404.png;
}
在后端端(例如PHP) -只需添加到url purgecache参数即可。但是值需要随机化,以禁用浏览器缓存:
<img src="/my/file.jpg?purgecache=<?=rand(1000,1000000000)?>">
或者只有服务器端清除:
file_get_contents(__DIR__ . "/my/file.jpg?purgecache=".rand(1000,1000000000));
https://stackoverflow.com/questions/74653910
复制相似问题