Static websites

On this page

A static site needs only the executable and a directory:

photon serve ~/Sites/example          # http://127.0.0.1:8080/, index.html for every directory

(see Serve a folder), or a config file next to the site for compression, caching, error pages and logs:

{
  "root": "public",
  "compression": true,
  "caching": { "maxAge": 600 },
  "errorPages": { "404": "errors/404.html" },
  "accessLog": { "path": "logs/access.log", "format": "combined" }
}

Every key is in the config reference. This page explains what the static components do.

StaticFiles

Caching and conditional requests

curl -sI http://127.0.0.1:8080/ | grep -i etag                     # ETag: "a9c-…"
curl -sI -H 'If-None-Match: "a9c-…"' http://127.0.0.1:8080/         # HTTP/1.1 304

Range requests

A single Range: bytes=… answers 206 Partial Content with Content-Range; an unsatisfiable range answers 416. Several ranges in one request are ignored (full 200). If-Range with a validator that no longer matches serves the whole file. This is what video seeking and resumed downloads need.

curl -s -r 0-99 -o part http://127.0.0.1:8080/video.mp4 -w '%{http_code}\n'    # 206

Error pages

errorPages gives bodiless 4xx/5xx responses an HTML body: a small generated page by default (<h1>404 Not Found</h1>), or your own files:

{ "errorPages": { "404": "errors/404.html", "500": "errors/500.html" } }

Compression

"compression": true turns on Brotli, gzip and deflate, negotiated from Accept-Encoding (q-values; br > gzip > deflate at equal weight), with Vary: Accept-Encoding.

Virtual hosts

Several sites on one listener, chosen by Host (HTTP/1.1) or :authority (HTTP/2, HTTP/3):

{
  "hosts": [
    { "names": ["example.com", "www.example.com"], "root": "sites/example" },
    { "names": ["*.blog.example.com", "blog.example.com"], "root": "sites/blog",
      "proxy": [{ "path": "/api", "upstream": "http://127.0.0.1:9000" }] },
    { "default": true, "root": "sites/fallback" }
  ]
}
KeyTypeMeaning
hosts[].namesarray of stringsHost names, matched without the port, case-insensitively. *.a.test matches any subdomain of a.test (not a.test itself)
hosts[].namestringOne name (alternative to names)
hosts[].rootstring (required)The site's directory, relative to the config file
hosts[].defaultboolThis site answers unknown hosts and requests without a host. At most one
hosts[].proxyarrayProxy mounts for this site only, tried before the top-level proxy mounts

Authentication

auth protects path prefixes with HTTP Basic, JWT bearer tokens or static bearer tokens:

{
  "auth": [
    { "paths": ["/admin"], "realm": "Admin", "basic": { "file": "users.htpasswd" } },
    { "paths": ["/api/private"], "realm": "API",
      "jwt": { "keys": [{ "id": "k1", "secretEnv": "JWT_KEY" }], "issuer": "https://id.example.com", "audience": "api" },
      "tokens": [{ "name": "ci", "sha256": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" }] }
  ]
}
KeyTypeMeaning
pathsstring or arrayPath prefixes starting with /. The first area covering a path decides
realmstringShown in the WWW-Authenticate challenge
basicstring or objectA credential file name, or {"file", "users", "usersEnv"}: lines user:$pbkdf2-sha256$… from a file, inline, or an environment variable. Plaintext passwords are refused
jwt.keysarray{"id"?, "secret" | "secretEnv"}; HS256 secrets of at least 32 bytes
jwt.issuer, jwt.audiencestring; string or arrayRequired iss / aud when set
jwt.leewayinteger 0–3600Clock skew allowed, seconds
jwt.requireExpboolDefault true: tokens need exp
tokensarrayStatic bearer tokens as {"name", "sha256"} (hex SHA-256 of the token; the token itself is never stored)

An area needs at least one of basic, jwt or tokens.

Make a Basic credential line (PBKDF2-SHA256, 600,000 iterations by default):

scripts/hash-password.sh alice >> users.htpasswd      # prompts for the password

Make a token digest: printf %s "$TOKEN" | shasum -a 256.

Behaviour: missing or wrong credentials get 401 with one challenge per accepted scheme; passwords are compared in constant time; a client address with 10 failures within 60 seconds gets 429 with Retry-After before any hashing; requests outside every area pass after one prefix check. Credentials are never logged. Basic sends the password with every request, so use it only over HTTPS. Auth runs before the proxy, so it also protects proxied paths; the upstream receives the Authorization header unchanged.