HTTP server setup and routing
Anubis accepts HTTP requests, prepares the client and proxy context, and sends each request to either an internal endpoint, the protection flow, or the protected origin. It also exposes a separate health and metrics service so operators can check service state without passing through request protection. See request processing for the wider request lifecycle, policy evaluation for the decision step, and challenge lifecycle for challenge state.
Server boundary
The server boundary connects a configured network listener to the request handler. The main service can use TCP or a Unix socket, while the metrics service uses the same listener setup rules on its own address.
This diagram shows how a request enters Anubis and reaches either an internal route, the protection decision, or the origin.
flowchart LR
L[HTTP or Unix listener] --> M[Request middleware]
M --> R[Anubis route handler]
R --> I[Internal endpoint]
R --> P[Protection decision]
P --> C[Challenge or denial]
P --> O[Protected origin]
Listener lifecycle
The listener setup accepts explicit network and address values or derives them from a URL-like address. TCP listeners are reported as HTTP endpoints. Unix listeners also apply the configured socket mode before the server starts. The HTTP server applies bounded header and idle time limits and shuts down with a short grace period when the process stops. internal/setuplistener.go• L13-91, internal/http_server.go• L8-15, cmd/…/main.go• L471-519
References: internal/setuplistener.go• L13-91 internal/http_server.go• L8-15 cmd/…/main.go• L471-519
Handler middleware
The main handler is wrapped before it reaches route matching. The wrapper chain can set the client address, normalize forwarded-for data, and add request fingerprints needed by policy checks. These values become part of the context used by the protection flow and by the origin request.
References: cmd/…/main.go• L471-479 internal/headers.go• L50-110 internal/headers.go• L113-147
Request normalization
Request normalization makes paths and client identity consistent before routing and policy evaluation. It is important because a proxy deployment may add a base path or hide the original network address behind one or more forwarding layers.
Base prefix routing
An optional base prefix places all Anubis routes below a shared path. Internal route registration uses that prefix, and the request router sends prefixed static, style, and robot resources to the internal mux. When a passed request is sent to the origin, an optional setting removes the prefix so the origin sees its normal path.
The prefix must start with / and must not end with /. Enabling prefix removal without a prefix is rejected at startup because it cannot define a safe path transformation.
References: lib/config.go• L171-215 lib/http.go• L457-501 cmd/…/main.go• L365-373
Client address headers
Anubis uses X-Real-IP as the normalized client address for downstream checks. Depending on the deployment, it can derive that value from the socket peer, from X-Forwarded-For, or from a configured custom header. Forwarded-for input is reduced to a trusted usable value and is updated before proxying. Unix-socket requests do not add a network address to the forwarded chain.
These rules keep policy checks and origin logs aligned with the deployment's proxy model. They also mean that incorrect proxy trust settings can change the client identity seen by the protection flow.
References: internal/headers.go• L20-32 internal/headers.go• L50-110 internal/headers.go• L113-147 internal/headers.go• L155-223
Route classes
The main mux separates Anubis-owned resources from requests that need protection. Internal routes are handled before the general route, so static assets and control endpoints do not become origin requests.
Internal routes
The internal mux serves static assets, optional robots.txt resources, optional imprint content, challenge actions, the status-only check endpoint, and challenge-specific resources. The general / route is the fallback for protected traffic. All registered paths include the configured base prefix.
The metrics service is separate from this mux. Its /metrics and /healthz endpoints are therefore not subject to the main protection route.
References: lib/config.go• L171-215 lib/http.go• L457-473 xess/xess.go• L34-39 lib/…/metrics.go• L35-64
Protected route handoff
Requests that are not internal resources reach the protection entry point. That entry point evaluates the request, then returns a denial or challenge, or calls the next handler after a valid pass. A status-only route uses the same decision path but can redirect to an external challenge flow instead of rendering a page.
This handoff is the boundary between HTTP routing and policy evaluation. Read policy evaluation for how the decision is made and challenge lifecycle for what happens after a challenge is issued.
References: lib/config.go• L213-215 lib/anubis.go• L277-283 lib/anubis.go• L328-368
Proxy handling
Proxy handling sends passed requests to the configured origin while preserving the request meaning needed by the upstream service. It supports normal HTTP targets and Unix-socket targets, with optional control over the upstream host name, TLS name, certificate checks, and connection reuse.
Origin forwarding
The reverse proxy rewrites the destination to the configured target and keeps the inbound host unless an explicit target host is set. It preserves the raw query and the forwarding headers that describe the original request. A Unix-socket target changes only the transport path; the origin still receives an HTTP request with a usable host.
TLS settings control certificate checks and the server name used during the upstream connection. These settings affect whether the origin connection succeeds and which certificate name the origin expects.
References: cmd/…/main.go• L140-216 lib/http.go• L210-223
Passed request delivery
After a request passes, Anubis marks the request with downstream risk headers and sends it to the next handler. If a base prefix is configured, the path is stripped at this boundary when enabled. A one-use referer cookie can also restore the original referer before the request reaches the origin.
The next handler is absent in authentication-only mode. In that mode, a passed request receives a success page or a redirect rather than proxy delivery.
References: lib/anubis.go• L424-445 lib/anubis.go• L456-461 lib/http.go• L504-532
Health endpoints
Health reporting separates process readiness from protected application traffic. The main service starts as not serving, changes to serving after its listener is ready, and changes back during shutdown.
Metrics health endpoint
The metrics service exposes /healthz and maps the Anubis service state to a simple HTTP result. A serving state returns 200 OK; a not-serving state returns 500; an unknown state returns 424. The command-line health check calls this endpoint and fails unless it receives 200.
The same service exposes Prometheus metrics. Optional debug profiling and metrics authentication belong to this operator-facing listener rather than the protected request listener.
References: lib/…/metrics.go• L35-64 lib/…/metrics.go• L102-129 cmd/…/main.go• L110-121 cmd/…/main.go• L281-284 cmd/…/main.go• L514-519
Response behavior
Response behavior sets cache and transfer metadata according to the response's role. Protected pages and errors must not be stored, while versioned static assets can use long-lived caching in release builds.
Response headers
The protection flow publishes the selected rule, action, and result through X-Anubis-Rule, X-Anubis-Action, and X-Anubis-Status. It removes matching connection tokens before setting these headers so an upstream cannot treat them as hop-by-hop values. Challenge and error pages use no-store; static assets use a long cache period outside development builds.
References: lib/anubis.go• L285-325 internal/headers.go• L36-47 internal/headers.go• L226-231 lib/http.go• L391-395 lib/http.go• L430-454
Gzip compression
Challenge pages are compressed only when the client advertises gzip support. The response keeps the gzip content marker and reuses writers across requests to reduce the cost of repeated challenge responses. Clients without gzip support receive the original response body.
References: internal/gzip.go• L12-64 lib/http.go• L305-309 lib/http.go• L391-395
Change checklist
When these files or sections are changed, remember to consider these:
- Keep base-prefix registration and prefix stripping aligned. A mismatch can make internal routes work while sending the wrong path to the origin.
- Keep the forwarded-header middleware order aligned with proxy rewrite behavior. The origin must receive the normalized values after they are computed.
- Keep
/healthztied to the Anubis service state and preserve the200contract used by the command-line health check. - Keep downstream risk headers out of
Connectiontokens so they remain end-to-end headers. - Keep status-only routing and page routing on the same protection decision path.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A base prefix lacks a leading slash or is combined with prefix stripping without a prefix | Startup fails rather than serving routes with an ambiguous path | Validate the prefix settings before constructing the server |
| Forwarded headers do not match the deployment's trusted proxy model | Policy checks and origin logs can use the wrong client address | Configure the real-IP source and forwarded-for behavior together |
| The health state remains not serving or unknown | Load balancers and the command-line health check mark the service unhealthy | Set serving state only after the listener is ready and expose it through /healthz |
| The proxy target is invalid or its TLS settings do not match the origin | Passed requests fail before reaching the protected service | Validate the target at startup and use matching host and TLS name settings |
| A client does not support gzip while a challenge path assumes it does | The challenge may be rejected as an unsupported client | Keep content negotiation before challenge rendering |
Key files
| File | Why to read it |
|---|---|
| cmd/…/main.go | Shows service startup, middleware assembly, proxy creation, listener setup, and health state changes. |
| lib/config.go | Shows the internal route map and the boundary between Anubis routes and protected traffic. |
| lib/http.go | Shows top-level request routing, base-prefix handling, passed-request delivery, and challenge response behavior. |
| lib/anubis.go | Shows the route-level handoff into policy decisions and the origin delivery outcomes. |
| internal/headers.go | Shows how client identity and forwarded headers are normalized. |
| internal/setuplistener.go | Shows supported listener forms and Unix-socket permission handling. |
| lib/…/metrics.go | Shows the separate health and metrics listener. |
| internal/gzip.go | Shows response compression behavior for challenge pages. |