Skip to Content
DNAbyD3Docs
Middleware

nginx

Put DNA in front of every request nginx serves, with plain nginx or OpenResty.

Add DNA to the locations that serve your site and nginx asks it about every request before your site sees it. A request DNA keeps out gets a 403; everything else reaches your site as usual.

Choose an install

Your nginxInstallYou add
Any nginx from your distro's packagesauth_requestA config block, no modules
OpenResty, or a gateway on it (Kong, APISIX)Lua scriptOne script and one line

Price routes with Payments? Use the Lua script: the auth_request install serves a priced route as an ordinary page.

Both need an API key.

auth_request

Confirm your nginx has the module (every distro package does):

nginx -V 2>&1 | grep auth_request
  1. Open the config in the adapters repo: nginx-auth-request/d3-edge.conf.

  2. Merge it into your config: the upstream block once, and the location = /_d3_edge block into each server you want covered.

  3. Add the check to every location that serves your site, not only location /: /api/, a ~ \.php$ block and any other. The file shows the pattern on location /:

    • auth_request /_d3_edge; and error_page 500 = @<fallback>; inside the location;
    • a named @<fallback> location with auth_request off; and the same handler as the location it mirrors (its proxy_pass, fastcgi_pass or root), so your site keeps serving if DNA can't be reached.

    A location without these lines serves its requests without DNA.

  4. Replace d3_k_REPLACE_ME with your key. nginx config can't read environment variables, so restrict the file as you would any config with a credential.

  5. Test and reload:

    nginx -t && nginx -s reload
  6. Verify the install on a page from each location.

A request DNA keeps out gets nginx's own 403 page.

Lua script

Needs nginx with Lua: OpenResty, or a build with ngx_http_lua_module, plus the lua-resty-http library.

  1. Save the script from d3-dna-adapters/openresty.
  2. From the sample config, add the env lines and the http {} settings.
  3. Add access_by_lua_file once in your server block, so every location inherits it.
  4. Set DNA_API_KEY where nginx starts.
  5. Reload nginx and verify the install.

With DNA_API_KEY unset, the script lets every request through. The repo README documents every variable.

Response shape

Every response has an x-d3-edge header: disabled, pass, block, fail-open, or fail-closed. A blocked request gets a 403 with a JSON body:

{ "blocked": true, "ruleId": "…", "tier": "…", "identity": "…" }

Upstream headers

On pass, the script sets these request headers for your upstream.

HeaderValue
X-D3-TierWho is asking, as a tier code
X-D3-IdentityThe agent's identity, at that tier
  • The script sets them on every pass, so a client can't spoof them past it.
  • Your application can act on them, for example to serve a lighter page to a crawler or skip analytics for bots.

On this page