Hardening
Security headers, access control, basic auth, hidden files, and request limits
The edge is where most attacks arrive. A few lines of Nginx config block whole classes of them before they reach application code.
Security headers
strict-transport-security max-age=63072000
x-content-type-options nosniff
x-frame-options DENY
referrer-policy strict-origin-when-cross-origin
x-request-id 55207536e3f85e621c0255d6a1bc9e30
server nginxalways adds them to error pages too, which attackers see as much as
normal ones. A Content-Security-Policy is the most powerful header,
but it has to be written for the specific app (which scripts, styles and
connections it uses), so it's usually set by the app.
server_tokens off reduces the Server header to just nginx, with no
version number for scanners to match against known vulnerabilities.
Unknown host names
SNI evil.example: TLSV1_UNRECOGNIZED_NAME
Host: evil.example 421The catch-all default server (see Configuration) refuses names the site doesn't own. Without it, anyone pointing a domain at your IP gets your site under their name, and Host-header attacks (poisoned password-reset links, cache poisoning) get a target.
Hidden files
location ~ /\. {
return 404; # .env, .git/, .htpasswd ... never served
}
GET /.env: 404There's a real html/.env in this module's web root to prove it. The
minimal compose.yaml, which uses the image's default config, serves it:
docker compose up -d # minimal: the official image's default.conf
curl -s -o /dev/null -w '%{http_code}' localhost:8090/.env
200Defaults serve every file in the root, dotfiles included. Leaked
.git directories and .env files from web roots are among the most
common causes of exposed credentials. Better still: build artifacts into
the web root, never deploy from a source checkout.
An admin area: network AND password
GET /admin/: 401 | with password: 200Allow rules depend on knowing the real client address. From the host,
requests arrive from the Docker gateway (172.30.0.1), which is inside
the allowed range, so "only the Docker network" also means "anyone who
can reach the published port." Behind a load balancer without realip,
every request has the load balancer's address. Treat IP rules as one
layer, never the only one.
Basic auth sends the password with every request (base64, not encrypted),
so only ever over HTTPS. For anything serious, put admin tools behind your
identity provider (an auth_request to an OAuth2 proxy).
Request limits
upload 5 MB: 200
upload 15 MB: 200
upload 25 MB: 413 # client_max_body_size 20m on /api/upload; 10m elsewhere| Directive | Protects against |
|---|---|
client_max_body_size | Huge uploads filling disks or memory (413) |
client_body_timeout, client_header_timeout | Slow clients holding connections open (Slowloris) |
limit_conn, limit_req | Floods, brute force (Rate Limiting) |
large_client_header_buffers | Oversized headers and cookies |
Over HTTP/2, the 25 MB upload didn't produce a clean 413 in the lab: Nginx stopped reading, the client waited on HTTP/2 flow control until its own timeout, and the lab now sends uploads over HTTP/1.1. Clients uploading large files should check size limits before sending, or use presigned direct uploads to object storage (the SeaweedFS module).
Lab
- Add
location /admin/access from a second Docker network and test from a container on it. - Put a
.git/HEADfile inhtml/and request it. - Add a
Content-Security-Policy: default-src 'self'header and load the home page in a browser. What does the console say about the inline script?