Skip to content
rtlbdPublic

About

Hardened non-root nginx Docker image with HTTP/3 (QUIC), HTTP/2, Brotli and post-quantum TLS. Multi-arch, built from source on Alpine.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

33 Commits

Folders and files

Repository files navigation

nginx

CI Release Version GHCR Docker Hub Docker pulls Image size License Last commit

HTTP/3 HTTP/2 Non-root Platforms Base Vulnerability scan Dependabot

A small, hardened nginx image built from source: HTTP/1.1, HTTP/2 and HTTP/3 (QUIC) on the standard ports, running entirely as a non-root user.

docker run -d --name nginx \
  -p 80:80/tcp -p 443:443/tcp -p 443:443/udp \
  imzami/nginx:latest

Open https://localhost/. With no certificate mounted the image generates a self-signed one, so the browser will warn; see Certificates.

The same image, with the same tags, is published to two registries:

Registry Image
Docker Hub imzami/nginx (docker.io/imzami/nginx)
GitHub Container Registry ghcr.io/rtlbd/nginx

Features

Protocols

HTTP/1.1, HTTP/2, HTTP/3 All three on every HTTPS site. HTTP/3 is advertised to browsers through the Alt-Svc header.
QUIC UDP 443 with reuseport and generic segmentation offload (quic_gso).
TLS 1.2 and 1.3 only Modern AEAD cipher suites; older protocols are refused.
Post-quantum key exchange X25519MLKEM768 hybrid group is preferred, with classical fallbacks.
HTTP → HTTPS redirect Port 80 answers with 308 to the same URL on HTTPS.
IPv4 and IPv6 The default site enables its IPv6 listeners automatically when the container has IPv6 (with a read-only root filesystem it stays on IPv4; list [::] in your own site files).

Ports: 80/tcp, 443/tcp, 443/udp.

Security

  • Non-root. Master and worker processes run as UID/GID 101 (nginx). There is no root process in the container at any point.
  • Read-only root filesystem. Only /tmp has to be writable.
  • No capabilities needed. Runs with --cap-drop=ALL and --security-opt no-new-privileges.
  • No version disclosure. server_tokens off, and the Server header is removed completely.
  • Security headers as a snippet: X-Content-Type-Options, X-Frame-Options, Referrer-Policy; HSTS as a separate opt-in snippet.
  • Rate and connection limits per client IP, ready to switch on with one include.
  • Strict catch-all server example that refuses the TLS handshake for unknown host names.
  • Pinned sources. The nginx tarball is verified by SHA-256 and the modules are pinned to full git commits.
  • Small. About 32 MB, Alpine based.

Performance

  • Brotli and gzip compression, including pre-compressed .br / .gz files.
  • sendfile, tcp_nopush, long keep-alives, open-file cache, PCRE JIT.
  • One worker per CPU core, 4096 connections per worker.
  • TLS session cache shared between workers.

Operations

  • Health check built in. HEALTHCHECK uses an internal listener on 127.0.0.1:8080, so it keeps working whatever site configuration you mount. stub_status is on the same listener at /nginx_status.
  • Works out of the box. A self-signed certificate is generated at start-up if none is mounted.
  • Environment templates. /etc/nginx/templates/*.template files are rendered with envsubst into /etc/nginx/conf.d/.
  • Start-up hooks. Executable *.sh files in /docker-entrypoint.d/ run before nginx.
  • Logs to stdout/stderr, with an optional JSON access-log format.
  • Graceful shutdown on docker stop (SIGQUIT).
  • Multi-arch. linux/amd64, linux/arm64 and linux/arm/v7 (32-bit ARM, such as a Raspberry Pi with a 32-bit OS).

Compiled-in modules

http_ssl, http_v2, http_v3, http_realip, http_gzip_static, http_gunzip, http_stub_status, http_auth_request, http_addition, http_sub, http_slice, http_secure_link, stream, stream_ssl, stream_ssl_preread, stream_realip, plus the third-party ngx_brotli and headers-more.

Running it hardened

services:
  nginx:
    image: imzami/nginx:latest
    restart: unless-stopped
    ports:
      - "80:80/tcp"
      - "443:443/tcp"
      - "443:443/udp"   # HTTP/3 (QUIC)
    read_only: true
    cap_drop: [ALL]
    security_opt: ["no-new-privileges:true"]
    tmpfs:
      - /tmp
    volumes:
      - ./conf.d:/etc/nginx/conf.d:ro
      - ./certs:/etc/nginx/certs:ro

The same file is in examples/docker-compose.yml.

Binding ports 80 and 443 without root

Docker allows unprivileged processes to bind low ports inside a container's own network namespace, so the default bridge network and Compose need nothing extra.

Two setups do need a setting:

  • Host networking (--network host): the host's own limit applies. Set net.ipv4.ip_unprivileged_port_start=0 on the host.

  • Kubernetes: add the sysctl to the pod.

    securityContext:
      runAsNonRoot: true
      runAsUser: 101
      sysctls:
        - name: net.ipv4.ip_unprivileged_port_start
          value: "0"

HTTP/3 checklist

  • Publish UDP 443 as well as TCP 443, and open it in the firewall or security group.
  • The Alt-Svc header must name the port clients connect to. If you publish a different port (for example -p 8443:443/udp), edit snippets/http3.conf to match.
  • Browsers only switch to HTTP/3 with a certificate they trust. A self-signed certificate keeps them on HTTP/2.

Configuration

Path Purpose
/etc/nginx/nginx.conf Main configuration: TLS, compression, logging, limits.
/etc/nginx/conf.d/*.conf Your sites. The image ships default.conf; replace it.
/etc/nginx/snippets/ Reusable pieces to include (below).
/etc/nginx/certs/ Certificates.
/etc/nginx/templates/ *.template files rendered into conf.d at start-up.
/usr/share/nginx/html/ Static files of the default site.
/docker-entrypoint.d/ Start-up scripts.

Snippets

Snippet Use in What it does
snippets/http3.conf server Alt-Svc header announcing HTTP/3.
snippets/security-headers.conf server nosniff, frame and referrer policies.
snippets/hsts.conf server Strict-Transport-Security for one year.
snippets/rate-limit.conf server / location 20 requests/s (burst 40) and 50 connections per IP.
snippets/proxy.conf location Reverse-proxy headers and WebSocket upgrade.
snippets/static-cache.conf location One-year immutable caching for fingerprinted assets.

add_header_inherit merge is set globally, so headers from these snippets are kept when a location adds its own.

Adding a site

Start from examples/site.conf (reverse proxy) and examples/catch-all.conf (strict default server). One rule matters for HTTP/3:

reuseport may be given only once per address and port. The default server carries it; every other site uses plain listen 443 quic;.

Certificates

The default site reads /etc/nginx/certs/fullchain.pem and /etc/nginx/certs/privkey.pem. Mount your own there; they must be readable by UID 101.

docker run -d -p 80:80 -p 443:443/tcp -p 443:443/udp \
  -v /path/to/certs:/etc/nginx/certs:ro \
  imzami/nginx:latest

If both files are missing, a self-signed certificate is created at start-up. With a read-only root filesystem that needs a writable /etc/nginx/certs, for example --tmpfs /etc/nginx/certs:uid=101,gid=101.

After renewing a certificate, reload without dropping connections:

docker exec nginx nginx -s reload

Environment variables

Variable Default Effect
NGINX_SELF_SIGNED_CN localhost Host name in the generated self-signed certificate.
NGINX_ENTRYPOINT_QUIET_LOGS unset Set to 1 to silence start-up script output.
NGINX_ENVSUBST_TEMPLATE_DIR /etc/nginx/templates Where templates are read from.
NGINX_ENVSUBST_OUTPUT_DIR /etc/nginx/conf.d Where rendered files are written.
NGINX_ENVSUBST_TEMPLATE_SUFFIX .template Suffix stripped from template names.

In templates, ${NAME} is replaced only when NAME is set in the environment, so nginx's own variables ($host, $uri) are left alone.

JSON access logs

access_log /var/log/nginx/access.log json;

Releasing

Pushing a version tag publishes the image to GitHub Container Registry and, once it is set up, to Docker Hub:

git tag v1.31.10
git push origin v1.31.10

The release workflow builds the image, runs the smoke test and the vulnerability scan, then builds for amd64, arm64 and arm/v7 and pushes it to every configured registry with an SBOM and build provenance attached. Both registries always get the same image and the same tags.

Docker Hub

Releases go to imzami/nginx. To set it up:

  1. On Docker Hub, create the repository nginx under imzami.

  2. In this GitHub repository, open Settings → Secrets and variables → Actions and add:

    Name Kind Value
    DOCKERHUB_USERNAME variable or secret imzami
    DOCKERHUB_PASSWORD secret the Docker Hub password of that user
    DOCKERHUB_IMAGE variable, optional only to push somewhere else, such as myorg/nginx; defaults to <DOCKERHUB_USERNAME>/nginx, here imzami/nginx
  3. Push a release tag, or run the Release workflow manually to publish the current release there too.

With neither DOCKERHUB_USERNAME nor DOCKERHUB_PASSWORD set, releases go to GHCR only and the run shows a warning. Setting only one of them fails the release, so a typo cannot silently skip Docker Hub.

CI also logs in to Docker Hub with the same credentials when they are set, which lifts the anonymous pull limit for the Alpine base image.

Version tags

A release tag is the nginx version, v<nginx version>. For another release of the same nginx (a configuration fix, for example) append a revision number: v1.31.10-1, then v1.31.10-2.

Git tag Image tags
v1.31.10 1.31.10, 1.31, 1, latest
v1.31.10-2 1.31.10-2, 1.31.10, 1.31, 1, latest
v1.31.11-rc.1 (anything else) 1.31.11-rc.1 only

The workflow refuses a release tag that does not match NGINX_VERSION in the Dockerfile.

Weekly security rebuild

Every Monday the workflow scans the latest published release. If any of its Alpine packages has a fix available, that release is rebuilt from the same source with current packages, tested, and republished under the same image tags on every registry. If nothing needs fixing, nothing is published.

Image tags therefore move to newer builds over time. To stay on one exact build, pin the image by digest (imzami/nginx@sha256:..., or the same digest on ghcr.io/rtlbd/nginx).

To rebuild on demand, run the Release workflow manually from the Actions tab; that always rebuilds the latest release.

The first publish creates the package as private. To make it pullable without login, open the package on GitHub → Package settings → Change visibility.

Upgrading nginx

All versions are set at the top of the Dockerfile:

Line What to set
FROM alpine:<version> Alpine release (Dependabot proposes updates).
NGINX_VERSION Version from https://nginx.org/en/download.html.
NGINX_SHA256 sha256sum of that nginx-<version>.tar.gz.
NGX_BROTLI_COMMIT Full commit of google/ngx_brotli.
HEADERS_MORE_COMMIT Full commit of openresty/headers-more-nginx-module.

Change them, push, let CI pass, then tag a release with the new nginx version.

Development

docker build -t nginx:dev .
tests/smoke.sh nginx:dev

The smoke test starts the image read-only with all capabilities dropped and checks the user, all three HTTP versions, compression, headers, the health check and graceful shutdown. The HTTP/3 check needs pip install -r tests/requirements.txt; without it that one check is skipped.

CI runs on every push and pull request:

  • Lint: hadolint for the Dockerfile, ShellCheck for the scripts, actionlint for the workflows.
  • Build and smoke test on amd64 and arm64 on native runners, and on arm/v7 under QEMU emulation.
  • Vulnerability scan with Trivy: fails on any finding in the image's Alpine packages that has a fix available, whatever its severity. The build runs apk upgrade, so every fixed package Alpine has published is in the image. nginx itself and the two modules are compiled from source, so they are not covered by the scan; keep them current through the pinned versions above.
  • Docker Scout scan with the same rule, when the Docker Hub credentials are set. Both scanners check every platform (amd64, arm64, arm/v7). Scout is what Docker Hub shows for the image and its data can differ from Trivy's, so both have to pass. The log also shows Scout's current report for the published image.

Dependabot opens weekly pull requests for new versions of the GitHub Actions, the Alpine base image and the test dependencies.

Differences from the official image

Official nginx This image
User root master, nginx workers everything as UID 101
HTTP/3 compiled in, not configured configured on the default site
Brotli, headers-more not included compiled in
Server header nginx removed
Read-only root filesystem needs extra mounts needs only /tmp
pid and temp files /var/run, /var/cache/nginx /tmp
Dynamic modules (njs, GeoIP, image filter, XSLT, ACME) available in the full variant not included

License

The files in this repository are released under the MIT License.

The image contains software under its own licenses, among others nginx (BSD-2-Clause), ngx_brotli (BSD-2-Clause), Brotli (MIT), headers-more (BSD-2-Clause), OpenSSL (Apache-2.0) and the Alpine Linux packages.

About

Hardened non-root nginx Docker image with HTTP/3 (QUIC), HTTP/2, Brotli and post-quantum TLS. Multi-arch, built from source on Alpine.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages