【发布时间】:2015-11-13 23:13:58
【问题描述】:
我正在努力为我的开源图像托管服务 PictShare 缓存图像。
Pictshare 有一个智能查询系统,其中上传的图像可以位于更改图像的“虚拟子目录”中。例如,这是上传的 stackoverflow 徽标的链接:https://www.pictshare.net/6cb55fe938.png
我可以通过将/300/ 添加到图像名称前的 URL 来将其调整为 300 宽度:https://www.pictshare.net/300/6cb55fe938.png
由于我最近处理大量流量,我希望我的 nginx 代理能够缓存所有虚拟子文件夹中的所有图像,但它不起作用。我已经阅读了许多文章和许多 stackoverflow 帖子,但没有适合我的解决方案。
到目前为止,这是我的高效虚拟主机文件
proxy_cache_path /etc/nginx/cache/pictshare levels=1:2 keys_zone=pictshare:50m max_size=1000m inactive=30d;
proxy_temp_path /etc/nginx/tmp 1 2;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_ignore_headers "Set-Cookie";
proxy_hide_header "Set-Cookie";
proxy_buffering on;
server {
...
location / {
proxy_pass http://otherserver/pictshare/;
include /etc/nginx/proxy_params;
location ~* \.(?:jpg|jpeg|gif|png|ico)$ {
expires max;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_valid 200 301 302 1y;
proxy_cache pictshare;
proxy_pass http://otherserver/pictshare$request_uri;
}
}
}
问题是没有文件被缓存,我看到代理目标上的每个图像请求。
我让它工作的唯一方法是向已明确启用缓存的主机文件添加一个特殊位置:
location /cached {
proxy_cache_valid 200 1y;
proxy_cache pictshare;
expires 1y;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
include /etc/nginx/proxy_params;
proxy_pass http://otherserver/pictshare/thumbs/;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
}
这个解决方案的一个明显问题是图像只在请求以/cached 开头时才被缓存,例如:https://www.pictshare.net/cached/6cb55fe938.png
将缓存命令添加到根目录不是我的选择,因为我不希望缓存表单和页面,只缓存图像
我的错误在哪里?
【问题讨论】: