Log inSign up

On-the-fly image resizing with imgproxy

Run the ghcr.io/imgproxy/imgproxy container on 127.0.0.1:8080 with a random hex key and salt, limit it to your own image origins with IMGPROXY_ALLOWED_SOURCES, and put Nginx in front with a disk cache and a certificate for img.example.net. Your site then requests URLs such as /SIGNATURE/rs:fit:600:0/plain/https://cdn.example.net/photo.jpg@webp and gets a resized WebP back.

Topic: Self-hosted apps · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Why run an image server like imgproxy?

imgproxy takes an original image from a URL or a local folder, processes it with the libvips library and returns the result, so you store one master file and request any size or format through the address. It is fast and frugal with memory, which suits a small VPS well.

Typical uses are responsive thumbnails for a shop or blog, converting uploads to WebP or AVIF, and smart cropping for preview cards. Because every variant is computed on request, put a cache in front of it; that is part of this guide.

Requirements are modest: 1 vCPU and 1 GB of memory serve a small site, and 2 vCPU with 2 to 4 GB handle frequent large originals. You need Docker (Docker Engine tutorial, or the ready Docker image) and a DNS name such as img.example.net.

How do you generate the signing key and salt?

Signed URLs stop outsiders from asking your server to resize arbitrary images in endless sizes, which would burn CPU and transfer. Create two random hex strings:

openssl rand -hex 32
openssl rand -hex 32

The first becomes the key, the second the salt. Store them in /opt/imgproxy/.env with mode 600, and give the same values to the application that builds image URLs:

IMGPROXY_KEY=first-hex-value
IMGPROXY_SALT=second-hex-value
IMGPROXY_ALLOWED_SOURCES=https://cdn.example.net/,local://
IMGPROXY_LOCAL_FILESYSTEM_ROOT=/images
IMGPROXY_MAX_SRC_RESOLUTION=40
IMGPROXY_QUALITY=82
IMGPROXY_TTL=2592000

IMGPROXY_ALLOWED_SOURCES keeps the server from fetching images from anywhere else. IMGPROXY_MAX_SRC_RESOLUTION (in megapixels) rejects absurdly large originals, and IMGPROXY_TTL sets the Cache-Control lifetime in seconds.

How do you start imgproxy with Docker Compose?

Save this as /opt/imgproxy/compose.yaml. The volume exposes a folder of originals read-only; leave it out if all images come from a remote origin.

services:
  imgproxy:
    image: ghcr.io/imgproxy/imgproxy:latest
    restart: unless-stopped
    env_file: .env
    ports:
      - "127.0.0.1:8080:8080"
    volumes:
      - /srv/images:/images:ro
sudo mkdir -p /srv/images
cd /opt/imgproxy
docker compose up -d
docker compose images
curl -s http://127.0.0.1:8080/health

The health endpoint replies that imgproxy is running. docker compose images shows which tag you are on; pin that tag in the Compose file once you are happy with it.

How do you build and sign an imgproxy URL?

A URL has three parts: a signature, processing options and the source. The signature is an HMAC-SHA256 of the salt followed by the path, using the key, encoded as URL-safe base64 without padding. This shell snippet computes it (install xxd with sudo apt install xxd, or sudo dnf install vim-common on Rocky and Alma):

KEY=first-hex-value
SALT=second-hex-value
P="/rs:fit:600:0/plain/local:///shop/mug.jpg@webp"
SIG=$( { echo -n "$SALT" | xxd -r -p; printf %s "$P"; } | openssl dgst -sha256 -mac HMAC -macopt hexkey:$KEY -binary | base64 | tr +/ -_ | tr -d =)
echo "http://127.0.0.1:8080/$SIG$P"
curl -s -o /tmp/mug.webp -w "%{http_code} %{content_type}\n" "http://127.0.0.1:8080/$SIG$P"

Copy a test image to /srv/images/shop/mug.jpg first. A 200 image/webp reply proves both signing and processing work. In the options, rs:fit:600:0 means resize to fit 600 pixels wide with automatic height; rs:fill:300:300 crops to a square and g:sm adds smart gravity for the crop. Client libraries for PHP, Python, Node.js and others produce the same signatures, so your application rarely needs this shell form.

How do you cache results and add HTTPS with Nginx?

imgproxy itself keeps no cache, so Nginx stores each processed variant on disk and serves repeats without touching the container. Install Nginx and Certbot (sudo apt install nginx certbot python3-certbot-nginx; on Rocky or Alma install epel-release first, use dnf, and run sudo setsebool -P httpd_can_network_connect 1). Then write /etc/nginx/conf.d/imgproxy.conf:

proxy_cache_path /var/cache/nginx/imgproxy levels=1:2 keys_zone=imgcache:50m
                 max_size=5g inactive=30d use_temp_path=off;

server {
    listen 80;
    listen [::]:80;
    server_name img.example.net;
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_cache imgcache;
        proxy_cache_valid 200 30d;
        proxy_cache_lock on;
        proxy_cache_use_stale error timeout updating;
        add_header X-Cache-Status $upstream_cache_status;
    }
}
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d img.example.net
sudo ufw allow 80,443/tcp

On Rocky and Alma use sudo firewall-cmd --permanent --add-service=https --add-service=http and sudo firewall-cmd --reload instead of ufw. Request the same signed URL twice through https://img.example.net with curl -I; the second answer should carry X-Cache-Status: HIT.

How do you serve WebP and AVIF automatically?

Two approaches exist. The explicit one, used above, appends @webp or @avif to the source, so each format has its own URL and caches cleanly. The automatic one sets IMGPROXY_AUTO_WEBP=true and IMGPROXY_AUTO_AVIF=true, after which imgproxy picks a format from the browser Accept header.

With automatic selection, the same URL returns different bytes per browser, so the Nginx cache key must include that choice. A small map keeps the key short:

map $http_accept $img_fmt {
    default "";
    "~image/avif" "avif";
    "~image/webp" "webp";
}

Place the map above the server block and add proxy_cache_key "$scheme$host$request_uri$img_fmt"; to the location. AVIF encoding costs noticeably more CPU than WebP, which is another reason the cache matters.

How do you keep imgproxy safe and fast?

  • Keep the key and salt secret; anyone with them can request any size from your allowed sources.
  • Limit originals with IMGPROXY_MAX_SRC_RESOLUTION and, for remote sources, IMGPROXY_MAX_SRC_FILE_SIZE in bytes.
  • Set IMGPROXY_WORKERS to about twice the number of vCPU if you need to cap concurrency.
  • Watch CPU and outgoing transfer with usage monitoring; a busy image host is often limited by transfer before CPU.

To update, pull the newer tag and recreate the container with docker compose pull and docker compose up -d. Clear the Nginx cache only when output really changes, for example after a quality setting change: sudo rm -rf /var/cache/nginx/imgproxy/* followed by sudo systemctl reload nginx.

What do typical imgproxy errors mean?

  • 403 Invalid signature: key or salt differ between imgproxy and the code that signs, or the path was changed after signing (including URL encoding of the source).
  • 404 or 422 for a source: the origin is not listed in IMGPROXY_ALLOWED_SOURCES, the file does not exist, or the origin returned an error.
  • Source image resolution is too big: raise IMGPROXY_MAX_SRC_RESOLUTION carefully or shrink the master.
  • Every request is a MISS: the cache key includes a changing header or the response sets no-cache; read docker compose logs imgproxy and the Nginx error log.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2thumbnails for one site1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4many originals and AVIF output2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

Questions about imgproxy image resizing

Can imgproxy read images from object storage?

Yes, it supports S3-compatible and other storage back ends through its own settings. We do not offer object storage, so point it at your existing bucket or use the local folder method.

Should I disable signatures during development?

Without a key and salt imgproxy accepts the word insecure in place of a signature. Use that only on a private test box, never on a public host.

Does imgproxy strip metadata?

By default it removes EXIF and similar metadata from results, which shrinks files and avoids leaking camera or location details.

Can I add a watermark?

Set IMGPROXY_WATERMARK_PATH to an image inside the container and use the wm option in the URL to place it with chosen opacity and position.

How large should the Nginx cache be?

Start with a few gigabytes and check du -sh /var/cache/nginx/imgproxy after a week. Leave room on disk for logs and originals as well.