nginx

The repository ships nginx configurations for all three deployment scenarios. They share one snippet for the proxied locations and add the internal accel locations only when you opt into nginx-served blobs. This page explains how to install them and why each directive is there.

The files

FileInstall asPurpose
examples/nginx/fs-direct.conf/etc/nginx/conf.d/vela.confScenario: filesystem, Go streams contents
examples/nginx/fs-accel.conf/etc/nginx/conf.d/vela.confScenario: filesystem, nginx streams contents
examples/nginx/r2.conf/etc/nginx/conf.d/vela.confScenario: R2/S3 proxy or redirect
examples/nginx/common.conf/etc/nginx/snippets/common.confProxied locations shared by every scenario
examples/nginx/accel-locations.conf/etc/nginx/snippets/accel-locations.confInternal locations for fs-accel only

Include exactly one scenario file in nginx's http context. The snippets are included from inside the scenario's server block; never put them in conf.d or include them directly in http.

sudo install -d /etc/nginx/snippets
sudo install -m 0644 examples/nginx/common.conf /etc/nginx/snippets/common.conf
# fs-accel only:
sudo install -m 0644 examples/nginx/accel-locations.conf /etc/nginx/snippets/accel-locations.conf

sudo install -m 0644 examples/nginx/fs-direct.conf /etc/nginx/conf.d/vela.conf
sudoedit /etc/nginx/conf.d/vela.conf    # replace store.example.com, certificate paths, upstream
sudo nginx -t && sudo systemctl reload nginx

The scenario file

This is fs-direct.conf. The accel and R2 files differ only in their header comment and, for accel, one extra include at the end.

# Never log query strings: signed links carry their signatures there.
log_format vela_noquery '$remote_addr - [$time_local] "$request_method $uri $server_protocol" '
                        '$status $body_bytes_sent "$http_user_agent" $request_time $request_id';

upstream vela {
    # The container publishes its port on loopback only:
    #   docker run -p 127.0.0.1:8080:8080 ...
    server 127.0.0.1:8080;
    keepalive 16;
}

server {
    listen 80;
    listen [::]:80;
    server_name store.example.com;

    access_log /var/log/nginx/vela.access.log vela_noquery;
    error_log  /var/log/nginx/vela.error.log;

    # certbot --webroot -w /var/www/certbot -d store.example.com
    location /.well-known/acme-challenge/ { root /var/www/certbot; }
    location / { return 301 https://$host$request_uri; }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name store.example.com;
    server_tokens off;

    ssl_certificate     /etc/letsencrypt/live/store.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/store.example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:vela_ssl:10m;
    ssl_session_timeout 1d;

    # Keep in sync with VELA_MAX_UPLOAD (2GiB by default).
    client_max_body_size 2g;

    # Uploads carry percent-encoded metadata in headers.
    large_client_header_buffers 4 64k;

    access_log /var/log/nginx/vela.access.log vela_noquery;
    error_log  /var/log/nginx/vela.error.log;

    include /etc/nginx/snippets/common.conf;
    # fs-accel only:
    # include /etc/nginx/snippets/accel-locations.conf;
}

Why each directive is there

log_format vela_noquery

Logs $uri instead of $request, so the query string — which holds signed-link signatures — never reaches the access log. It is used in both server blocks, including the HTTP redirect server, because signed links can arrive over plain HTTP too.

upstream vela … keepalive 16

Reuses connections to velad. Change the address if you set VELA_HOST_PORT or run the container elsewhere.

return 301 https://$host$request_uri

$request_uri includes the query string, so an http:// signed link still works after the redirect.

client_max_body_size 2g

Must match VELA_MAX_UPLOAD. A smaller proxy limit makes large uploads fail with nginx's own 413 before they reach Vela.

large_client_header_buffers 4 64k

Upload metadata travels in percent-encoded headers. The longest allowed description (4096 four-byte characters) encodes to a 48 KiB header line; nginx's default of 8 KiB per line would reject valid uploads with 400. velad itself accepts up to 128 KiB of headers.

server_tokens off

Hides the nginx version in responses and error pages.

The shared locations

# R2 proxy delivery must not spill large response bodies onto the server disk.
proxy_buffering off;
proxy_max_temp_file_size 0;
proxy_read_timeout 600s;
proxy_send_timeout 600s;

proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# One ID in the nginx access log and the velad log for the same request.
proxy_set_header X-Request-ID $request_id;

# velad sets X-Content-Type-Options itself; hide it and add it once here.
proxy_hide_header X-Content-Type-Options;

location /api/ {
    proxy_pass http://vela;
    proxy_request_buffering off;

    add_header Strict-Transport-Security "max-age=31536000" always;
    add_header X-Content-Type-Options nosniff always;
}

location /s/ {
    proxy_pass http://vela;
    client_max_body_size 1k;
    error_log /dev/null;

    add_header Strict-Transport-Security "max-age=31536000" always;
    add_header X-Content-Type-Options nosniff always;
}

# Everything else (index, direct URLs, theme assets, web sign-in) is
# decided by velad. Do not add proxy_cache here: with VELA_WEB_AUTH, pages
# and files depend on the session cookie (velad marks them Vary: Cookie and
# signed-in responses private, no-store).
location / {
    proxy_pass http://vela;
    # The public side never accepts request bodies.
    client_max_body_size 1k;

    add_header Strict-Transport-Security "max-age=31536000" always;
    add_header X-Content-Type-Options nosniff always;
}
proxy_request_buffering off

Streams upload bodies straight to velad instead of writing them to an nginx temp file first. Vela enforces size limits and computes the SHA-256 while streaming.

proxy_buffering off · proxy_max_temp_file_size 0

Responses are streamed, never spooled to disk — important for R2 proxy mode on a small VPS disk. Long read/send timeouts accommodate large transfers.

X-Real-IP · X-Forwarded-For · X-Request-ID

Always overwritten by the proxy. velad trusts them only from VELA_TRUSTED_PROXIES; see trusted proxies.

error_log /dev/null in /s/

nginx's error log always records the full request line, query included — for example when the upstream is down. Signed-link failures still show up in velad's log, which never records queries.

client_max_body_size 1k in / and /s/

The public side never accepts bodies, so there is no reason to read one. The optional web sign-in form posts a single short key field, which fits comfortably.

add_header … always

An add_header inside a location replaces every inherited one, so the security headers are repeated per location. Hiding the upstream X-Content-Type-Options guarantees the header appears exactly once, even on nginx's own error pages.

nginx accel (fs-accel)

With accel, velad still authorizes every request, then answers with an empty body and X-Accel-Redirect: /_blobs/<shard>/<id>. nginx performs an internal redirect and serves the immutable blob itself with sendfile, ranges and HEAD. It is an optimization for heavy download traffic; access decisions never move to nginx.

Enabling it

  1. Set VELA_STORAGE=fs and VELA_ACCEL_PREFIX=/_blobs/ (the run-fs-accel.sh script does this).
  2. Install accel-locations.conf and include it after common.conf (the fs-accel.conf scenario already does).
  3. Make sure nginx can read the host blob directory, by default /srv/vela/blobs/. Vela creates blob directories 0755 and files 0644 for exactly this reason.

The internal locations

location /_blobs/ {
    internal;
    alias /srv/vela/blobs/;

    sendfile on;
    tcp_nopush on;

    # Validators come from velad: the ETag is the content's SHA-256.
    etag off;
    if_modified_since off;
    add_header ETag $upstream_http_x_vela_etag always;
    add_header Last-Modified "" always;

    # Re-emit policy headers that do not survive the internal redirect.
    add_header Cache-Control $upstream_http_x_vela_cache_control always;
    add_header X-Robots-Tag $upstream_http_x_vela_robots always;
    add_header Referrer-Policy $upstream_http_x_vela_referrer_policy always;
    # Set only with VELA_WEB_AUTH: responses vary by session cookie, and
    # uploaded HTML/SVG/XML is sandboxed away from that cookie.
    add_header Vary $upstream_http_x_vela_vary always;
    add_header Content-Security-Policy $upstream_http_x_vela_content_security_policy always;
    add_header X-Request-ID $upstream_http_x_request_id always;
    add_header Strict-Transport-Security "max-age=31536000" always;
    add_header X-Content-Type-Options nosniff always;
}

# Signed links: velad redirects them to /_blobs/s/<shard>/<id>
# so that error logging can be off here as well.
location /_blobs/s/ {
    internal;
    alias /srv/vela/blobs/;
    error_log /dev/null;
    # ... the same directives as above ...
}
  • internal — clients requesting /_blobs/ directly get 404. Only velad's redirect can reach it.
  • Validators — nginx's own ETag (mtime + size) can collide for same-size content replaced within a second, so it is disabled. velad sends the SHA-256 ETag as X-Vela-ETag, answers If-None-Match itself, and Last-Modified is removed.
  • X-Vela-* headers — Content-Type and Content-Disposition survive an internal redirect, but other upstream headers do not. velad passes cache, robots and referrer policies as X-Vela-Cache-Control, X-Vela-Robots and X-Vela-Referrer-Policy, which nginx re-emits. An empty variable adds no header — intended, since public files carry no X-Robots-Tag.
  • No direct Cache-Control — in accel mode velad never sends Cache-Control itself, or nginx would forward it and add the copy. An end-to-end test checks every header arrives exactly once.
  • /_blobs/s/ — shard directories have two-character names, so s/ can never clash with them.

Mismatched setupA non-empty VELA_ACCEL_PREFIX without the matching internal locations produces empty downloads or proxy 404s. The internal locations without the prefix do nothing — Go streams files instead. velad logs delivery=accel /_blobs/ at startup when accel is active. R2 storage with a non-empty accel prefix is rejected at startup.

Accel and web sign-in

The Vary and Content-Security-Policy lines are new in v0.3. nginx drops Vary from an X-Accel-Redirect response, so velad sends both as X-Vela-Vary and X-Vela-Content-Security-Policy and the internal locations re-emit them. They are empty — and add nothing — while web sign-in is off, so you can add them to both internal locations now and enable sign-in later. Enabling sign-in without them would serve uploaded HTML outside the sandbox.

R2 scenario

r2.conf is identical to fs-direct.conf: the bucket is private and every byte flows through velad (proxy mode) or the client is sent to a short-lived presigned URL (redirect mode). The inherited proxy_buffering off and proxy_max_temp_file_size 0 keep nginx from spooling bucket downloads to local disk. See Cloudflare R2 & S3.

Certificates

The configuration references Let's Encrypt paths as an illustration only. With certbot's webroot method:

sudo install -d /var/www/certbot
# Temporarily comment out the 443 server until the first certificate exists, then:
sudo certbot certonly --webroot -w /var/www/certbot -d store.example.com
sudo nginx -t && sudo systemctl reload nginx

Validating changes

  • Run nginx -t before every reload.
  • make validate-examples (in the repository, needs Docker and OpenSSL) checks all three unmodified scenarios in nginx:stable with temporary self-signed certificates, plus the Caddyfile.
  • make test-e2e runs real accel delivery through nginx and verifies headers, validators, caching, ranges and that logs contain no queries.

Do not replace vela_noquery with nginx's default combined format, and do not add another proxy or CDN layer that logs full URLs. Either would write working signed-link credentials to disk.

Caching and CDNs

Public and unlisted files are sent with Cache-Control: public, max-age=300; signed links and API downloads with private, no-store; invalid ranges with no-store. A shared cache that honors these headers is safe. Remember that a cached copy can outlive a visibility change by up to five minutes.

Do not add proxy_cache to the location / block. With web sign-in, pages and files depend on the session cookie: velad marks them Vary: Cookie and signed-in responses private, no-store.

CDNs and the session cookieCloudflare and similar CDNs cache by file extension and ignore Vary: Cookie. With web sign-in on, add a cache bypass rule for requests that carry the __Host-vela_session cookie — otherwise a signed-in response could be served to anonymous visitors, or the other way round.