Check if cookie does NOT contain specified content NGINX Check if cookie does NOT contain specified content NGINX nginx nginx

Check if cookie does NOT contain specified content NGINX


In Nginx, each cookie is available in embedded variable $cookie_CookieName. In case you want to check cookie with name mycookie, you can do it using this configuration snippet:

if ($cookie_mycookie != "foobar") {  return 401;}

From nginx manual for the if command:

A condition maybe (among others):

  • Comparison of a variable with a string using the = and != operators;

  • Matching of a variable against a regular expression using the ~ (for case-sensitive matching) and ~* (for case-insensitive matching) operators.

  • Regular expressions can contain captures that are made available for later reuse in the $1..$9 variables.

  • Negative operators !~ and !~* are also available. If a regular expression includes the } or ; characters, the whole expressions should be enclosed in single or double quotes.