Skip to content

Bot protection on NGINX and OpenResty

Validate requests in the access phase, before proxy_pass, so blocked scrapers and credential stuffers never reach your backend.

How it fits into NGINX

Execution phase
centinel-nginx.lua with lua-resty-http, set up in init_by_lua_block and run in access_by_lua_block.
Runtime support
OpenResty 1.19+ out of the box, or stock NGINX with libnginx-mod-http-lua, lua-cjson and lua-resty-string.
Timeouts and backoff
100 ms request and connect timeouts by default. Failures back off up to 5 minutes, tracked in lua_shared_dict centinel_cache.
Route scoping
Every non-static route by default, or Lua patterns in protected_paths such as ^/api/ and ^/admin.

How a request moves through NGINX

The module runs after the rewrite phase and before proxy_pass, so a blocked request never opens an upstream connection.

  1. init_by_lua_block

    centinel.init() runs once with your key, validator URL and timeouts. The centinel_cache shared dict holds backoff state across workers.

  2. access_by_lua_block

    centinel.access_handler() skips static assets and unmatched protected_paths, then calls the validator through lua-resty-http or the pooled /_centinel_validate location.

  3. Decision

    Allow lets NGINX continue to proxy_pass or content_by_lua_block. Block or challenge returns the validator's page from the access phase.

  4. Failure

    With fail_open on, the default, a validator error lets the request pass and backs off 1 s, 2 s, 4 s, 8 s and up to 5 minutes.

Minimal configuration and a live check

Minimal nginx.conf

nginx.conf
env CENTINEL_SECRET_KEY;

http {
    lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
    lua_ssl_verify_depth 3;
    lua_ssl_protocols TLSv1.2 TLSv1.3;
    resolver 8.8.8.8 valid=30s ipv6=off;
    lua_shared_dict centinel_cache 10m;
    lua_package_path '/usr/local/openresty/site/lualib/?.lua;;';

    init_by_lua_block {
        centinel = require("centinel-nginx")
        centinel.init({
            secret_key         = os.getenv("CENTINEL_SECRET_KEY"),
            timeout_ms         = 1000,
            connect_timeout_ms = 500
        })
    }

    server {
        listen 80;
        location / {
            access_by_lua_block { centinel.access_handler() }
            proxy_pass http://your-backend;
        }
    }
}

The 1,000 ms and 500 ms timeouts suit the lua-resty-http path. In Docker Compose, set the resolver to 127.0.0.11.

Reload and send a scripted request

Verify
nginx -t && nginx -s reload
curl -s -o /dev/null -w '%{http_code}\n' -A 'python-requests/2.31' http://localhost/api/test

Expect 403 once blocking is enabled, or 200 with a logged event in your Centinel dashboard while in log-only mode. Because nginx -t skips init_by_lua_block, set error_log to info while checking to confirm Centinel module initialized appears; a 500 on the first request usually means lua-resty-http is missing.

Deployment options

What to know before deploying

  • Do not use return as your content directive

    return runs in the rewrite phase, before access_by_lua_block, so Centinel never executes. Use proxy_pass or content_by_lua_block where Centinel should run.

  • Set the key in systemd, not your shell

    nginx -s reload does not copy shell variables into the master process. Declare env CENTINEL_SECRET_KEY; in nginx.conf and set the value in a systemd drop-in.

  • Debian 12 needs explicit TLS settings

    Older lua-nginx-module builds default to TLS 1.2 or lower. Set lua_ssl_protocols TLSv1.2 TLSv1.3, trusted CA certificates and a resolver for the validator call.

  • No shared dict means no backoff

    Without lua_shared_dict centinel_cache, the module logs a warning and runs with backoff disabled, so a failing validator is called on every request.

  • Your upstream servers and load-balancing pools
  • Your TLS termination, virtual hosts and proxy_cache keys
  • Static-asset location blocks and caching headers
  • Your existing ModSecurity or limit_req rules

NGINX integration questions

Walk through your NGINX setup

Bring one route bots are hitting. We will map the right connector, timeouts, and log-only rollout for your NGINX stack.