Request security processing
Request security processing is the request-time part of BunkerWeb. NGINX accepts HTTP or stream traffic, Lua builds a shared request view, and ordered security components decide whether the request is allowed, challenged, limited, denied, or sent to an upstream service. The generated configuration connects these phases to the runtime; see Configuration generation for how that configuration is produced and Runtime security engine for the wider runtime model.
Runtime request model
The gateway uses NGINX phases as control points. Lua components share request state and return a common result so that one phase can pass its decision to later phases.
HTTP lifecycle
An HTTP request is prepared before the access decision, then moves through access checks and content or proxy handling. A final log phase records the decision and lets components finish request-level work.
flowchart LR
A[HTTP request] --> B[TLS and request setup]
B --> C[Lua access checks]
C -->|allow| D[Local content or reverse proxy]
C -->|redirect, challenge, or deny| E[HTTP response]
D --> E
E --> F[Lua log processing]
The access phase fills the request context, refreshes shared cache state, checks early bans, and runs the ordered access methods. A component can return a status or redirect; the phase stops at that decision and saves the context for later use. (src/common/confs/server-http/access-lua.conf#L41-L75, src/common/confs/server-http/access-lua.conf#L78-L111, src/common/confs/server-http/access-lua.conf#L131-L218)
References: src/…/access-lua.conf• L41-75 src/…/access-lua.conf• L131-218
TLS handshake lifecycle
The TLS ClientHello is an earlier control point for selected client bans and security plugins. It can drop a connection before HTTP request processing, but it does not replace the access phase when the client identity is uncertain or the transport uses unsupported address information.
flowchart LR
A[ClientHello] --> B[Trusted client identity]
B --> C[Core ban decision]
C -->|drop| D[Handshake close]
C -->|allow or detect| E[Plugin ClientHello hooks]
E -->|drop| D
E -->|allow| F[TLS and request phases]
The handshake path is deliberately non-blocking. It skips a drop when it cannot prove the client address, when QUIC parameters are present, or when a local lookup is uncertain. Detect mode records a would-be drop and lets the handshake continue. Registered plugin hooks then apply their own early checks in a fixed order. (src/bw/lua/bunkerweb/banlease.lua#L714-L797, src/bw/lua/bunkerweb/banlease.lua#L800-L944)
References: src/…/banlease.lua• L714-797 src/…/banlease.lua• L800-944
Stream lifecycle
Stream traffic has no HTTP access phase. NGINX inspects the connection during preread, applies the stream plugin order, and then either closes the connection or passes it to the stream proxy.
flowchart LR
A[Stream connection] --> B[TLS certificate selection]
B --> C[Stream preread checks]
C -->|allow| D[Stream reverse proxy]
C -->|deny| E[Connection close]
D --> F[Stream log processing]
The preread phase uses the same context and decision pattern as HTTP. It checks bans before running plugin methods, which prevents known bad clients from reaching the upstream connection. (src/common/confs/server-stream/preread-stream-lua.conf#L26-L76, src/common/confs/server-stream/preread-stream-lua.conf#L78-L154)
References: src/…/preread-stream-lua.conf• L26-76 src/…/preread-stream-lua.conf• L78-154
Lua phase coordination
Lua provides the shared decision layer across NGINX and the security components. The runtime loads component metadata and phase order at initialization, then uses that order for each request.
Shared request context
The request context is the common record for client identity, service scope, request data, whitelist state, ban state, reason data, and session changes. Components read and update this record instead of passing separate values between phases. (src/common/confs/server-http/access-lua.conf#L61-L76, src/common/confs/server-http/log-lua.conf#L25-L35)
Shared dictionaries hold runtime data that must survive between worker requests. HTTP and stream traffic use separate stores where their NGINX subsystems need separate state; cache updates make changes from other workers visible during safe request phases. (src/common/confs/http.conf#L56-L74, src/common/confs/stream.conf#L25-L44)
References: src/…/access-lua.conf• L61-76 src/…/http.conf• L56-74 src/…/stream.conf• L25-44
Ordered plugin decisions
The initialization phase loads available components, reads the generated variables, computes global and per-site phase order, and stores that order in shared state. Request phases then call only the methods that a component provides. This lets security features add checks without changing the NGINX phase contract. (src/common/confs/init-lua.conf#L64-L115, src/common/confs/init-lua.conf#L144-L201)
Components can return an allow result, a deny status, or a redirect. In detect mode the runtime reports a would-be denial and continues; in block mode the result becomes the response or connection action. Whitelisting can still turn a detected or denied result into an allowed outcome. (src/common/confs/server-http/access-lua.conf#L158-L185)
References: src/…/init-lua.conf• L64-115 src/…/init-lua.conf• L144-201 src/…/access-lua.conf• L158-185
Security decisions
Security checks combine local policy, shared state, external decisions, and NGINX-native controls. The early checks protect the rest of the chain, while later checks add request-specific controls or produce a user-facing response.
Identity and early rejection
The client identity used by security checks comes from the connection after trusted proxy settings are applied. Real IP rules define which sources may replace the connection address, so whitelist, ban, rate, and reputation checks use a consistent client address. Ban and lease keys use one canonical address form, which prevents different IPv4 and IPv6 spellings from creating separate security records. (src/common/core/realip/confs/server-http/real-ip.conf#L1-L39, src/bw/lua/bunkerweb/banlease.lua#L16-L18, src/bw/lua/bunkerweb/banlease.lua#L277-L348)
The whitelist can match client IP, reverse DNS, ASN, user agent, URI, or a configured header. Its result is cached and exposed to both Lua and ModSecurity so that trusted traffic can bypass later checks. (src/common/core/whitelist/whitelist.lua#L169-L199, src/common/core/whitelist/whitelist.lua#L202-L253)
Known bans are checked before the normal plugin chain. A non-whitelisted banned client is denied in block mode; detect mode records the ban without applying the normal deny response. The optional TLS handshake check uses the same ban reasons and whitelist result, but drops only when the client address is proven. (src/common/confs/server-http/access-lua.conf#L78-L111, src/common/confs/server-stream/preread-stream-lua.conf#L50-L72, src/bw/lua/bunkerweb/banlease.lua#L719-L789)
Ban leases use short-lived local records and optional shared Redis records. Epochs fence a lookup or promotion that started before a removal, so a stale external decision cannot restore a ban after the operator removes it. (src/bw/lua/bunkerweb/banlease.lua#L522-L584, src/bw/lua/bunkerweb/banlease.lua#L612-L711)
References: src/…/real-ip.conf• L1-39 src/…/whitelist.lua• L169-253 src/…/access-lua.conf• L78-111 src/…/banlease.lua• L522-584
Traffic controls and external decisions
Traffic controls protect service capacity after the client passes early identity checks. Request limits can apply to a URL or to the whole service, while connection limits are enforced by NGINX for HTTP protocol streams and stream connections. (src/common/core/limit/limit.lua#L185-L300, src/common/core/limit/confs/server-http/limitconn.conf#L1-L5)
The CrowdSec integration asks a configured bouncer for a decision. It can deny a request, serve a challenge, or allow the request; a failed service setup skips CrowdSec for that service rather than stopping all traffic. The bouncer keeps enforcement records across worker restarts and configuration changes. It restores only valid state and refreshes it when a published revision changes. In stream mode, a missing published snapshot with a failed refresh fails closed to a ban rather than trusting stale state. (src/common/core/crowdsec/crowdsec.lua#L294-L339, src/common/core/crowdsec/lib/bouncer.lua#L648-L714, src/common/core/crowdsec/enforcement_state.lua#L254-L304)
ModSecurity is the NGINX-native web application firewall path. Its generated rules can be enabled per service and can use either service-specific or global rule files. Reverse-proxy locations may disable it when an endpoint needs different request-body handling. (src/common/core/modsecurity/confs/server-http/modsecurity.conf#L1-L10, src/common/core/reverseproxy/confs/server-http/reverse-proxy.conf#L126-L138)
References: src/…/limit.lua• L185-300 src/…/limitconn.conf• L1-5 src/…/crowdsec.lua• L294-339 src/…/enforcement_state.lua• L254-304 src/…/bouncer.lua• L648-714 src/…/modsecurity.conf• L1-10
Bot and client certificate checks
The antibot component uses a session-backed challenge to separate normal clients from suspected automation. It first applies ignore rules and cached results, then prepares a challenge session, displays the challenge, validates the submitted proof, and redirects a solved client to a safe same-origin path. Session failure returns an error instead of allowing an unchallenged request. (src/common/core/antibot/antibot.lua#L398-L555, src/common/core/antibot/antibot.lua#L590-L673, src/common/core/antibot/antibot.lua#L745-L1069)
Only a safe relative destination can be used after a challenge. The component rejects external targets, challenge loops, control bytes, and path aliases that resolve back to the challenge. It stores the full original URI in the session while carrying only a path in the redirect, so the query is restored only after the destination is matched. (src/common/core/antibot/antibot.lua#L95-L141, src/common/core/antibot/antibot.lua#L1107-L1187)
Mutual TLS (mTLS) uses the TLS handshake for site-wide client certificate checks. Optional verification allows Lua to apply certificate rules only to matching HTTP paths; a matching path without a valid certificate is denied. (src/common/core/mtls/confs/server-http/mtls.conf#L1-L20, src/common/core/mtls/mtls.lua#L113-L169)
References: src/…/antibot.lua• L95-141 src/…/antibot.lua• L398-555 src/…/antibot.lua• L1107-1187 src/…/mtls.conf• L1-20 src/…/mtls.lua• L113-169
Proxy behavior
Proxy behavior is the allowed outcome of request processing. NGINX applies the generated proxy rules only after security checks return control, then preserves the client and service context across the upstream boundary.
HTTP upstream delivery
HTTP reverse proxy locations map configured paths to upstream hosts. They control buffering, timeouts, request bodies, authentication subrequests, WebSocket upgrades, response headers, and optional proxy caching. (src/common/core/reverseproxy/confs/server-http/reverse-proxy.conf#L98-L128, src/common/core/reverseproxy/confs/server-http/reverse-proxy.conf#L140-L211)
The proxy forwards the trusted client address and request scheme through standard forwarding headers. When upstream TLS is enabled, SNI, certificate verification, trusted CA data, and an optional client certificate define the upstream trust boundary. (src/common/core/reverseproxy/confs/server-http/reverse-proxy.conf#L18-L48, src/common/core/reverseproxy/confs/server-http/reverse-proxy.conf#L142-L159)
References: src/…/reverse-proxy.conf• L98-128 src/…/reverse-proxy.conf• L140-211
Stream upstream delivery
Stream proxying sends an allowed connection to the configured backend without HTTP location rules. It can use upstream TLS settings and the PROXY protocol to preserve connection identity for the backend. (src/common/core/reverseproxy/confs/server-stream/reverse-proxy.conf#L1-L50)
TLS certificate selection runs before stream preread. It lets ordered components choose a certificate and key from the client SNI while retaining a default certificate when no component makes a selection. (src/common/confs/server-stream/ssl-certificate-stream-lua.conf#L47-L147)
References: src/…/reverse-proxy.conf• L1-50 src/…/ssl-certificate-stream-lua.conf• L47-147
Request outcomes
The runtime records why a request was allowed, challenged, redirected, limited, or denied. The log phase restores the shared context, runs component log methods, and writes the final reason with whitelist and security-mode information for operators. (src/common/confs/server-http/log-lua.conf#L20-L35, src/common/confs/server-http/log-lua.conf#L55-L106)
The default server has a separate log path for requests that do not match a configured service. This path preserves the reason and runs default-server component hooks, which is important when an early rejection happens before normal service handling. (src/common/confs/default-server-http.conf#L253-L358)
References: src/…/log-lua.conf• L20-35 src/…/log-lua.conf• L55-106 src/…/default-server-http.conf• L253-358
Change checklist
When these files or sections are changed, remember to consider these:
- Keep HTTP, stream, and TLS ClientHello identity rules aligned. A change to trusted PROXY protocol or real IP networks can change which client the early ban check sees.
- Keep canonical IP keys aligned across local bans, Redis leases, whitelist checks, and TLS handshake decisions.
- Preserve lease epoch fencing when changing ban promotion or removal. A result that returns after removal must not recreate the ban.
- When changing CrowdSec checkpoint publication, keep the revision marker, sequence order, and service binding rules consistent so readers do not use an incomplete snapshot.
- Keep antibot session destinations and redirect targets under the same same-origin validation rules. A new challenge flow must not trust a query-string target on its own.
- Keep early ban handling aligned in HTTP access and stream preread so a client cannot bypass a control by changing transport type.
- When changing proxy headers or real IP trust, review both the client identity used by security checks and the identity sent to the upstream.
- When changing mTLS from site-wide to path-specific checks, keep TLS verification mode compatible with the Lua access decision.
- When changing request-body limits, review both NGINX body limits and ModSecurity inspection limits for each proxy location.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The shared plugin order is missing or cannot be read | The phase cannot run its configured security methods | Initialization must store the order before workers serve requests; phase code logs the read failure. |
| A client is banned but whitelist state is stale | A valid whitelist could be ignored or a banned client could be allowed incorrectly | Ban handling performs an authoritative IP whitelist check before applying the ban. |
| A ban lookup or promotion returns after the ban is removed | A stale result could recreate an active lease | Lease and marker epochs invalidate work captured before removal. |
| Client identity depends on an untrusted PROXY protocol or real IP header | TLS early drop cannot prove the client and may not run | The handshake path skips the drop and leaves the access phase authoritative unless the peer is trusted. |
| CrowdSec enforcement state is unreadable or invalid | A stream request may use unsafe stale state or fail to get a decision | Checkpoint reads report the error; a missing published snapshot fails closed when refresh cannot succeed. |
| The antibot session cannot be read | The request could pass without a valid challenge state | The access method returns an internal error instead of allowing the request. |
| A challenge target is external or resolves to the challenge URI | Solved clients could be redirected off-site or into a loop | Redirect targets must be safe same-origin relative paths and fall back to /. |
| Trusted CA data is missing while upstream TLS verification is enabled | Upstream certificate verification is disabled for that proxy | The generated proxy config enables verification only when the trusted CA file exists. |
| Path-specific mTLS uses a handshake mode that rejects missing certificates | Clients fail during TLS before Lua can apply path rules | Use optional client verification when enforcement is per path. |
| A proxy location passes a large body through ModSecurity | The WAF connector can buffer too much request data and exhaust memory | Disable ModSecurity for that location or set a matching location body limit. |
Key files
| File | Why to read it |
|---|---|
src/common/confs/server-http/access-lua.conf | Shows the HTTP access decision and plugin chain. |
src/common/confs/server-stream/preread-stream-lua.conf | Shows the stream security decision before proxying. |
src/common/confs/init-lua.conf | Shows how runtime plugin order and shared data are prepared. |
src/bw/lua/bunkerweb/banlease.lua | Shows canonical client identity, lease fencing, TLS early drops, and ClientHello plugin hooks. |
src/common/core/whitelist/whitelist.lua | Shows the shared allow-list contract used by security checks. |
src/common/core/limit/limit.lua | Shows request-rate control and its block or detect result. |
src/common/core/crowdsec/crowdsec.lua | Shows external reputation decisions and challenge results. |
src/common/core/crowdsec/enforcement_state.lua | Shows how CrowdSec enforcement state is restored, validated, and published. |
src/common/core/crowdsec/lib/bouncer.lua | Shows how CrowdSec decisions use live state and restored enforcement state. |
src/common/core/antibot/antibot.lua | Shows the challenge, session, validation, and safe redirect lifecycle. |
src/common/core/mtls/mtls.lua | Shows path-specific client certificate enforcement. |
src/common/core/reverseproxy/confs/server-http/reverse-proxy.conf | Shows how allowed HTTP requests reach upstreams. |
src/common/core/reverseproxy/confs/server-stream/reverse-proxy.conf | Shows how allowed stream connections reach upstreams. |
src/common/confs/server-http/log-lua.conf | Shows how final outcomes are recorded. |