|
| 1 | +--- |
| 2 | +title: Cache |
| 3 | +description: Learn about how to configure and use imgproxy's internal cache |
| 4 | +--- |
| 5 | + |
| 6 | +# Cache ((pro)) |
| 7 | + |
| 8 | +imgproxy Pro provides an internal cache that stores processed images on disk or in cloud storage. This cache can act as both a primary cache and a secondary cache that serves as a fallback when your CDN cache misses. |
| 9 | + |
| 10 | +:::tip |
| 11 | +While putting a CDN in front of imgproxy is and will always be a best practice, the internal cache provides long-term storage for cases that require an additional caching layer. |
| 12 | +::: |
| 13 | + |
| 14 | +## Why use internal cache? |
| 15 | + |
| 16 | +The internal cache provides long-term persistent storage for processed images, unlike CDNs that typically delete rarely accessed content. It stores images in a single location rather than across multiple edge stores, eliminating cache misses when requests hit different edges. The cache is designed specifically for imgproxy, working seamlessly with features like modern image format detection and client hints support that generic external caches don't understand. |
| 17 | + |
| 18 | +The cache is protected by the same security measures as imgproxy itself, including URL signatures and processing restrictions. Importantly, URL signatures are not part of the cache key, so you can rotate keys or use multiple key/salt pairs without invalidating cached images. You maintain full control over where the cache is stored and how it integrates with your infrastructure. |
| 19 | + |
| 20 | +## Configuration |
| 21 | + |
| 22 | +You need to define the following config variables to enable the internal cache: |
| 23 | + |
| 24 | +* [`IMGPROXY_CACHE_USE`]: the cache storage adapter to use. Can be `filesystem`, `s3`, `gcs`, `abs` (Azure Blob Storage), or `swift`. When blank, the cache is disabled. Default: blank |
| 25 | +* [`IMGPROXY_CACHE_PATH_PREFIX`]: _(optional)_ a path prefix for the cache files. This can be useful to organize cache files in a specific directory structure. Default: blank |
| 26 | +* [`IMGPROXY_CACHE_BUCKET`]: _(optional)_ the bucket name for cloud storage adapters (S3, GCS, ABS, Swift). When using filesystem adapter, this can be used as an additional path component. Default: blank |
| 27 | +* [`IMGPROXY_CACHE_KEY_HEADERS`]: _(optional)_ a list of HTTP request headers (comma-separated) to include in the cache key. This allows caching different versions of the same image based on request headers. Default: blank |
| 28 | +* [`IMGPROXY_CACHE_KEY_COOKIES`]: _(optional)_ a list of HTTP request cookies (comma-separated) to include in the cache key. This allows caching different versions of the same image based on cookies. Default: blank |
| 29 | +* [`IMGPROXY_CACHE_REPORT_ERRORS`]: when `true`, imgproxy will report cache errors instead of silently falling back to processing without cache. Default: `false` |
| 30 | + |
| 31 | +### Storage configuration |
| 32 | + |
| 33 | +The internal cache supports all the storage backends that imgproxy can read source images from: local filesystem, Amazon S3 and compatible services (Cloudflare R2, DigitalOcean Spaces, MinIO, etc.), Google Cloud Storage, Microsoft Azure Blob Storage, and OpenStack Swift. |
| 34 | + |
| 35 | +Configure the storage backend using the same configuration options as for image sources, but with the `IMGPROXY_CACHE_` prefix instead: |
| 36 | + |
| 37 | +* For filesystem cache, use [`IMGPROXY_CACHE_LOCAL_FILESYSTEM_ROOT`](../configuration/options.mdx#cache-storage-local-filesystem) |
| 38 | +* For S3 cache, use `IMGPROXY_CACHE_S3_*` [variables](../configuration/options.mdx#cache-storage-amazon-s3) (e.g., `IMGPROXY_CACHE_S3_REGION`, `IMGPROXY_CACHE_S3_ENDPOINT`) |
| 39 | +* For GCS cache, use `IMGPROXY_CACHE_GCS_*` [variables](../configuration/options.mdx#cache-storage-google-cloud-storage) |
| 40 | +* For Azure Blob Storage cache, use `IMGPROXY_CACHE_ABS_*` [variables](../configuration/options.mdx#cache-storage-azure-blob-storage) |
| 41 | +* For Swift cache, use `IMGPROXY_CACHE_SWIFT_*` [variables](../configuration/options.mdx#cache-storage-openstack-swift) |
| 42 | + |
| 43 | +See the [configuration options](../configuration/options.mdx#cache) documentation for the full list of available variables. |
| 44 | + |
| 45 | +## Examples |
| 46 | + |
| 47 | +### Filesystem cache |
| 48 | + |
| 49 | +```bash |
| 50 | +IMGPROXY_CACHE_USE=filesystem |
| 51 | +IMGPROXY_CACHE_LOCAL_FILESYSTEM_ROOT=/var/cache/imgproxy |
| 52 | +IMGPROXY_CACHE_PATH_PREFIX=images |
| 53 | +``` |
| 54 | + |
| 55 | +### S3 cache |
| 56 | + |
| 57 | +```bash |
| 58 | +IMGPROXY_CACHE_USE=s3 |
| 59 | +IMGPROXY_CACHE_BUCKET=my-imgproxy-cache |
| 60 | +IMGPROXY_CACHE_S3_REGION=us-east-1 |
| 61 | +# AWS credentials are provided via IAM role or environment variables |
| 62 | +``` |
| 63 | + |
| 64 | +### Google Cloud Storage cache |
| 65 | + |
| 66 | +```bash |
| 67 | +IMGPROXY_CACHE_USE=gcs |
| 68 | +IMGPROXY_CACHE_BUCKET=my-imgproxy-cache |
| 69 | +IMGPROXY_CACHE_GCS_KEY=/path/to/credentials.json |
| 70 | +``` |
| 71 | + |
| 72 | +### Azure Blob Storage cache |
| 73 | + |
| 74 | +```bash |
| 75 | +IMGPROXY_CACHE_USE=abs |
| 76 | +IMGPROXY_CACHE_BUCKET=imgproxy-cache |
| 77 | +IMGPROXY_CACHE_ABS_NAME=mystorageaccount |
| 78 | +IMGPROXY_CACHE_ABS_KEY=your_account_key |
| 79 | +``` |
| 80 | + |
| 81 | +## Cache key |
| 82 | + |
| 83 | +The cache key is generated based on: |
| 84 | + |
| 85 | +* Source image URL |
| 86 | +* Processing options |
| 87 | +* Output format |
| 88 | +* Optional: Request headers specified in `IMGPROXY_CACHE_KEY_HEADERS` |
| 89 | +* Optional: Request cookies specified in `IMGPROXY_CACHE_KEY_COOKIES` |
| 90 | + |
| 91 | +URL signature is **not** a part of the cache key, which allows key rotation without invalidating the cache. |
| 92 | + |
| 93 | +## Limitations |
| 94 | + |
| 95 | +* **No manual cache invalidation**: Currently, imgproxy doesn't provide built-in means to invalidate the cache. However, imgproxy includes the [cachebuster](../usage/processing.mdx#cache-buster) in the cache key, so you can use it to force cache invalidation when needed. Most storage offerings also support object expiration, so you can set a reasonable expiration time for cached images. |
| 96 | +* **No cache for info requests**: The internal cache is currently only used for image processing requests. Requests to the `/info` endpoint are not cached. |
| 97 | + |
| 98 | +## How it works |
| 99 | + |
| 100 | +When a request comes in: |
| 101 | + |
| 102 | +1. imgproxy checks the URL signature (if enabled) |
| 103 | +2. imgproxy generates the cache key from the request parameters |
| 104 | +3. imgproxy checks if a cached image exists in the configured storage |
| 105 | +4. If the cached image exists and is valid, imgproxy serves it directly |
| 106 | +5. If not, imgproxy processes the image and stores the result in the cache before serving it |
| 107 | + |
| 108 | +## Monitoring |
| 109 | + |
| 110 | +Monitor cache performance with metrics: |
| 111 | + |
| 112 | +* Cache hit rate |
| 113 | +* Cache size |
| 114 | +* Cache evictions |
| 115 | +* Cache latency |
| 116 | + |
| 117 | +These metrics are available through [monitoring endpoints](../monitoring/prometheus.mdx). |
0 commit comments