CORS & Host filtering
Allow cross-origin requests with CORS and restrict which Host headers servd accepts.
CORS
--cors enables Cross-Origin Resource Sharing. Pass * to allow any origin, or list explicit origins (repeatable):
servd --static=./dist --cors=*
servd --static=./dist --cors=https://app.example.com --cors=https://admin.example.com
Behavior:
- With
*, every response getsAccess-Control-Allow-Origin: *. - With explicit origins, the request
Originis normalized and echoed back only when it matches, andVary: Originis added. - Credentials are never enabled.
servddoes not sendAccess-Control-Allow-Credentials, so cookies and HTTP auth are not shared cross-origin. - Preflight (
OPTIONS) requests are answered with204and the allowed method/headers. For a static mount onlyGET/HEADare allowed; proxied mounts allow the requested method. - When CORS is enabled and a route is a proxy, any
Access-Control-*headers coming from the upstream are stripped soservdstays authoritative and headers are not duplicated.
An Origin of null (for example from a file:// page or a sandboxed iframe) is only allowed under --cors=*.
Host filtering
--host restricts which Host request headers are accepted (repeatable):
servd --static=./dist --host=localhost --host=app.example.com
- A request whose
Hostis not in the list gets403 Forbidden. - Values are normalized (lowercased, trailing dot removed, IPv6 unwrapped). A host entry must not include a port.
- With no
--host, all hosts are accepted (Hosts: unrestrictedin the startup output).
Host filtering is a routing/guard convenience. It does not configure DNS and provides no authentication — anyone who can reach the port can send a matching
Hostheader. Use it to avoid serving content for unexpected virtual hosts, not as a security boundary.
Combining both
servd \
--spa=./frontend \
--proxy=/api/=http://localhost:9000/api/ \
--host=localhost \
--cors=http://localhost:5173
This serves the app, proxies the API, only answers requests addressed to localhost, and allows the Vite dev origin to call it cross-origin during development.