|
| 1 | +--- |
| 2 | +description: External cache options for imgproxy, including CDNs, nginx, and Varnish |
| 3 | +--- |
| 4 | + |
| 5 | +# External cache |
| 6 | + |
| 7 | +External cache refers to caching layers in front of imgproxy, such as CDNs, reverse proxies, or caching appliances. These solutions provide edge caching, geo-distribution, and high-performance image delivery. |
| 8 | + |
| 9 | +## CDN options |
| 10 | + |
| 11 | +Most major CDNs (Cloudflare, Fastly, AWS CloudFront, Akamai, etc.) can cache imgproxy responses: |
| 12 | + |
| 13 | +### Recommended CDN settings |
| 14 | + |
| 15 | +* **Image optimization** - Disable CDN image optimization to avoid double-processing |
| 16 | +* **Compression** - In case your CDN supports conditional compression of SVG images, enable gzip/Brotli compression for bandwidth savings |
| 17 | +* **Include essential headers*** - Include `Accept` header in the cache key if you're using [format negotiation](../configuration/options.mdx#avifwebpjpeg-xl-support-detection). If you have [client hints](../configuration/options.mdx#client-hints-support) enabled, include also `DPR`, `Sec-CH-Dpr`, and `Sec-CH-Width` headers. |
| 18 | +* **Origin shield** - Add a secondary cache layer between your CDN and imgproxy origin if your CDN supports it. |
| 19 | + |
| 20 | +## nginx caching |
| 21 | + |
| 22 | +nginx can act as a reverse proxy cache in front of imgproxy: |
| 23 | + |
| 24 | +```nginx |
| 25 | +proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=imgproxy_cache:10m max_size=50g inactive=30d; |
| 26 | +
|
| 27 | +upstream imgproxy { |
| 28 | + server localhost:8080; |
| 29 | +} |
| 30 | +
|
| 31 | +server { |
| 32 | + listen 80; |
| 33 | + server_name images.example.com; |
| 34 | +
|
| 35 | + # Uncomment if you do not use format negotiation and client hints |
| 36 | + # proxy_cache_key "$scheme$proxy_host$request_uri"; |
| 37 | +
|
| 38 | + # Uncomment if you use format negotiation |
| 39 | + # proxy_cache_key "$scheme$proxy_host$request_uri$http_accept"; |
| 40 | +
|
| 41 | + # Uncomment if you use client hints |
| 42 | + # proxy_cache_key "$scheme$request_method$host$request_uri:dpr=$http_dpr:width=$http_width:chdpr=$http_sec_ch_dpr:chwidth=$http_sec_ch_width"; |
| 43 | +
|
| 44 | + # Uncomment if you use format negotiation and client hints |
| 45 | + # proxy_cache_key "$scheme$request_method$host$request_uri:accept=$http_accept:dpr=$http_dpr:width=$http_width:chdpr=$http_sec_ch_dpr:chwidth=$http_sec_ch_width"; |
| 46 | +
|
| 47 | + location / { |
| 48 | + proxy_pass http://imgproxy; |
| 49 | +
|
| 50 | + # Cache configuration |
| 51 | + proxy_cache imgproxy_cache; |
| 52 | + proxy_cache_valid 200 30d; |
| 53 | + proxy_cache_valid 404 1m; |
| 54 | + proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; |
| 55 | + proxy_cache_background_update on; |
| 56 | + proxy_cache_lock on; |
| 57 | + proxy_cache_bypass $http_pragma $http_authorization; |
| 58 | + proxy_no_cache $http_pragma $http_authorization; |
| 59 | +
|
| 60 | + # Add cache status header for debugging |
| 61 | + add_header X-Cache-Status $upstream_cache_status; |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### Possible cache key setup |
| 67 | + |
| 68 | +The exact key depends on which imgproxy features can change output for the same URL. |
| 69 | + |
| 70 | +1. Basic key when output depends only on URL/path options: |
| 71 | + |
| 72 | +```nginx |
| 73 | +proxy_cache_key "$scheme$proxy_host$request_uri"; |
| 74 | +``` |
| 75 | + |
| 76 | +2. Include [format negotiation](../configuration/options.mdx#avifwebpjpeg-xl-support-detection) signal when WebP/AVIF/JXL preference can vary by client: |
| 77 | + |
| 78 | +```nginx |
| 79 | +proxy_cache_key "$scheme$proxy_host$request_uri$http_accept"; |
| 80 | +``` |
| 81 | + |
| 82 | +3. Include [Client Hints](../configuration/options.mdx#client-hints-support) dimensions when width/DPR affects output: |
| 83 | + |
| 84 | +```nginx |
| 85 | +proxy_cache_key "$scheme$request_method$host$request_uri:accept=$http_accept:dpr=$http_dpr:width=$http_width:chdpr=$http_sec_ch_dpr:chwidth=$http_sec_ch_width"; |
| 86 | +``` |
| 87 | + |
| 88 | +Use the smallest key that still prevents collisions for your traffic profile. |
| 89 | + |
| 90 | +### Key recommendations |
| 91 | + |
| 92 | +* Include `Accept` in the cache key if [format negotiation](../configuration/options.mdx#avifwebpjpeg-xl-support-detection) is used. |
| 93 | +* Include DPR/width-related hints in the cache key if [Client Hints](../configuration/options.mdx#client-hints-support) are used to vary output. |
| 94 | +* Set long `inactive` timeout (30d) to keep popular images in cache. |
| 95 | +* Configure `proxy_cache_use_stale` for graceful degradation during origin issues. |
| 96 | +* Monitor `/proc/sys/fs/file-max` and increase if needed for large caches. |
| 97 | +* Use separate cache zones for different image types if needed. |
| 98 | + |
| 99 | +## Varnish caching |
| 100 | + |
| 101 | +Varnish is a powerful reverse proxy cache optimized for HTTP performance: |
| 102 | + |
| 103 | +```vcl |
| 104 | +vcl 4.1; |
| 105 | +
|
| 106 | +backend imgproxy { |
| 107 | + .host = "localhost"; |
| 108 | + .port = "8080"; |
| 109 | + .connect_timeout = 600ms; |
| 110 | + .first_byte_timeout = 600s; |
| 111 | + .between_bytes_timeout = 60s; |
| 112 | +} |
| 113 | +
|
| 114 | +sub vcl_recv { |
| 115 | + set req.backend_hint = imgproxy; |
| 116 | +
|
| 117 | + # Cache only GET and HEAD requests |
| 118 | + if (req.method != "GET" && req.method != "HEAD") { |
| 119 | + return (pass); |
| 120 | + } |
| 121 | +
|
| 122 | + # Include important headers in cache key |
| 123 | + if (req.http.Accept) { |
| 124 | + set req.http.X-Accept = req.http.Accept; |
| 125 | + } |
| 126 | +
|
| 127 | + # Keep DPR if Client Hints are not enabled |
| 128 | + if (req.http.DPR) { |
| 129 | + set req.http.X-DPR = req.http.DPR; |
| 130 | + } |
| 131 | +
|
| 132 | + if (req.http.Width) { |
| 133 | + set req.http.X-Width = req.http.Width; |
| 134 | + } |
| 135 | +
|
| 136 | + if (req.http.Sec-CH-DPR) { |
| 137 | + set req.http.X-CH-DPR = req.http.Sec-CH-DPR; |
| 138 | + } |
| 139 | +
|
| 140 | + if (req.http.Sec-CH-Width) { |
| 141 | + set req.http.X-CH-Width = req.http.Sec-CH-Width; |
| 142 | + } |
| 143 | +} |
| 144 | +
|
| 145 | +sub vcl_hash { |
| 146 | + # Base hash |
| 147 | + hash_data(req.url); |
| 148 | + hash_data(req.http.Host); |
| 149 | +
|
| 150 | + # Include Accept header for format negotiation |
| 151 | + if (req.http.X-Accept) { |
| 152 | + hash_data(req.http.X-Accept); |
| 153 | + } |
| 154 | +
|
| 155 | + # Include DPR if present |
| 156 | + if (req.http.X-DPR) { |
| 157 | + hash_data(req.http.X-DPR); |
| 158 | + } |
| 159 | +
|
| 160 | + if (req.http.X-Width) { |
| 161 | + hash_data(req.http.X-Width); |
| 162 | + } |
| 163 | +
|
| 164 | + if (req.http.X-CH-DPR) { |
| 165 | + hash_data(req.http.X-CH-DPR); |
| 166 | + } |
| 167 | +
|
| 168 | + if (req.http.X-CH-Width) { |
| 169 | + hash_data(req.http.X-CH-Width); |
| 170 | + } |
| 171 | +} |
| 172 | +
|
| 173 | +sub vcl_backend_response { |
| 174 | + # Cache successful responses for 30 days |
| 175 | + if (beresp.status == 200) { |
| 176 | + set beresp.ttl = 30d; |
| 177 | + set beresp.keep = 30d; |
| 178 | + } |
| 179 | +
|
| 180 | + # Cache 404s for a short period |
| 181 | + if (beresp.status == 404) { |
| 182 | + set beresp.ttl = 1m; |
| 183 | + } |
| 184 | +
|
| 185 | + # Enable cache using stale object if backend is down |
| 186 | + set beresp.grace = 24h; |
| 187 | +} |
| 188 | +
|
| 189 | +sub vcl_deliver { |
| 190 | + # Add cache hit/miss header |
| 191 | + if (obj.hits > 0) { |
| 192 | + set resp.http.X-Cache = "HIT"; |
| 193 | + set resp.http.X-Cache-Hits = obj.hits; |
| 194 | + } else { |
| 195 | + set resp.http.X-Cache = "MISS"; |
| 196 | + } |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +### Key recommendations |
| 201 | + |
| 202 | +* Use `sub vcl_hash` to customize cache key behavior. |
| 203 | +* Include `Accept` for [format negotiation](../configuration/options.mdx#avifwebpjpeg-xl-support-detection). |
| 204 | +* Include DPR/width-related headers when [Client Hints](../configuration/options.mdx#client-hints-support) support is enabled (`DPR`, `Width`, `Sec-CH-DPR`, `Sec-CH-Width`). |
| 205 | +* Set grace period (`beresp.grace`) for graceful error handling. |
| 206 | +* Monitor Varnish statistics with `varnishstat`. |
| 207 | +* Use tags for efficient cache purging: add `set beresp.http.Surrogate-Key = "images:product:123"` in `vcl_backend_response`. |
| 208 | +* Configure workspace limits if caching many large images. |
| 209 | + |
| 210 | +## Cache headers from imgproxy |
| 211 | + |
| 212 | +imgproxy sends the following headers useful for external caching: |
| 213 | + |
| 214 | +* `Cache-Control: public, max-age=31536000` - Long-term caching (1 year default, configurable with `IMGPROXY_TTL`) |
| 215 | +* `ETag` - If `IMGPROXY_USE_ETAG=true` (by default), enables conditional requests |
| 216 | +* `Last-Modified` - If `IMGPROXY_USE_LAST_MODIFIED=true` (by default), enables conditional requests |
| 217 | +* `Content-Type` - Important for cache key if not in `Accept` header |
0 commit comments