testcookie

The testcookie directive enables or disables cookie-based robot mitigation challenges in NGINX.

Syntaxtestcookie on | off | var;
Defaultoff
Contexthttp, server, location, if in server, if in location
Arguments1

Description

The "testcookie" directive is part of the NGINX testcookie robot mitigation module, which utilizes cookies to identify and mitigate potential bot traffic based on challenge-response mechanisms. When enabled, NGINX uses cookies to manage the challenge process, setting a cookie for clients upon their first visit. The directive specifies whether this mitigation method is active, thereby influencing how requests are handled.

With the directive set to "on", it enables the mechanism where a challenge cookie can be issued to clients. This cookie can be configured with various attributes such as expiration, name, domain, path, SameSite policies, and more, providing flexibility in how cookies are utilized for bot mitigation. The behavior of the directive can be influenced further through additional settings in the module (like specifying the input for challenge generation via the "testcookie_session" directive), allowing for tailored configurations that suit specific scenarios or traffic patterns.

In scenarios where bot traffic is suspected or encountered, the directive can help distinguish between legitimate users and robots, ensuring automated scripts do not improperly access website resources. However, proper configuration is essential, as improperly set cookie parameters or failure to specify the required "testcookie_session" directive can lead to ineffective mitigation or user access issues.

Config Example

http {
    testcookie on;
    testcookie_session "$remote_addr";
    testcookie_max_attempts 3;
    testcookie_fallback "https://example.com/fallback";
}

Ensure that you have configured the required 'testcookie_session' directive to avoid misconfigurations.

Be cautious with the 'testcookie_secret'; using 'random' can lead to cookie validation issues on server restarts as all existing cookies will be invalidated.

A lack of proper 'testcookie_arg' settings may cause infinite redirects, leading to poor user experience.

← Back to all directives