Learn Labs
Security

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

snippets/security-headers.confadd_header Strict-Transport-Security "max-age=63072000" always;HTTPS onlyadd_header X-Content-Type-Options "nosniff" always;no MIME guessingadd_header X-Frame-Options "DENY" always;no framing (clickjacking)add_header Referrer-Policy "strict-origin-when-cross-origin" always;add_header X-Request-ID $request_id always;for support and logs
  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                     nginx

always 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 421

The 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:         404

There'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
200

Defaults 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

location /admin/ {satisfy all;both rules must pass (any = either)allow 172.30.0.0/24;the compose networkallow 127.0.0.1;deny all;auth_basic "admin";auth_basic_user_file /etc/nginx/certs/htpasswd;created by certs.sh: openssl passwd -apr1}
GET /admin/:       401 | with password: 200

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
DirectiveProtects against
client_max_body_sizeHuge uploads filling disks or memory (413)
client_body_timeout, client_header_timeoutSlow clients holding connections open (Slowloris)
limit_conn, limit_reqFloods, brute force (Rate Limiting)
large_client_header_buffersOversized headers and cookies

Lab

  1. Add location /admin/ access from a second Docker network and test from a container on it.
  2. Put a .git/HEAD file in html/ and request it.
  3. 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?

On this page