在 nginx 1.10.1 上,我代理一个外部网站(不受我控制)在本地缓存图像。
我的配置如下:
location ~ /cachedimages/(?<productcode>.*)/(?<size>.*)/image.jpg {
resolver 127.0.0.1;
proxy_pass https://www.externalsite.example/api/getImage/?productcode=$productcode&size=$size;
proxy_cache imgcache;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
expires 1M;
access_log off;
add_header 'Cache-Control' "public";
add_header Last-Modified $upstream_http_last_modified;
add_header X-Proxy-Cache $upstream_cache_status;
}
Run Code Online (Sandbox Code Playgroud)
imgcache 定义如下:
proxy_cache_path /var/cache/nginx/imgcache levels=1:2 keys_zone=imgcache:10m max_size=1g inactive=24h;
Run Code Online (Sandbox Code Playgroud)
远程服务器不提供Last-Modified标题:
curl -X GET -I https://www.externalsite.example/api/getImage/?productcode=abc123&size=128
HTTP/1.1 200 OK
Date: Thu, 15 Sep 2016 08:16:07 GMT
Server: Apache
Transfer-Encoding: chunked
Content-Type: image/jpeg
Run Code Online (Sandbox Code Playgroud)
我的服务器添加了一些标题但没有 Last-Modified
curl -X GET -I https://www.myserver.com/cachedimages/abc123/128/image.jpg …Run Code Online (Sandbox Code Playgroud)