Minimal Dockerized NGINX + njs playground with 4 production-ish use cases:
- HMAC request signing (
/sign) – issuesX-Signbased on method+uri+msec using envHMAC_SECRET. - Access gate + canary/feature flag routing (
/api/hello) – requiresx-api-key, calls/_int/flagsand internally redirects to/v1/helloor/v2/hello. Control population with envFF_PERCENT(0-100). Sticky perCookie: user=.... - Response redaction (
/report-filtered) – masks long digit sequences from/reportusing an njs body filter. - Computed variables –
js_setpopulates$hmac,$bucket,$fffor logs/headers.
This is not Node.js; njs is a tiny embeddable JS engine for request-time glue logic. Keep logic fast (<1ms).
docker build -t nginx-njs-kit .
docker run --name nginx-njs-kit --restart=unless-stopped -d -e HMAC_SECRET=change-me -e FF_PERCENT=50 -p 8080:8080 nginx-njs-kit
# smoke
curl http://localhost:8080/GET /sign→ 204 + headersX-Time,X-SignGET /api/hello(needsx-api-key) → internally serves/v1/helloor/v2/hello- add
Cookie: user=aliceto get consistent bucketing - tune
FF_PERCENTto control v2 exposure
- add
GET /report→ raw JSON with PII-ish digitsGET /report-filtered→ redacted JSON via njs header/body filters
make build
make start
make stop
make logs
make reload
make ssl
make sshWindows PowerShell test:
.\scripts\test.ps1 -HostName localhost- The HMAC uses Node-like
cryptoif the distro's njs module provides it; otherwise it falls back to a demo base64url (do not use in prod). On Debian/Ubuntulibnginx-mod-http-njstypically includes crypto. - All demos are same-process (no real upstream). In a real reverse-proxy, replace internal redirects with
proxy_passand forward headers (e.g.,proxy_set_header X-Sign $hmac;). - Body filtering deletes
Content-Lengthto force chunked encoding, as required when body size changes.