检查 cookie 是否不包含指定内容 NGINX
Check if cookie does NOT contain specified content NGINX
关于检查 cookie 是否存在并包含我的内容的教程数不胜数,在本例中 foobar
。
假设 mycookie 是我要设置的 cookie,我该如何执行以下操作。
if ($cookie_mycookie does not equal "foobar") {
return 401;
}
我试过以下方法都无济于事。
if (!$http_mycookie ~* "foorbar" ) {
return 401;
}
谢谢!
在 Nginx 中,每个 cookie 都可以在嵌入式变量中使用 $cookie_CookieName
。如果您想检查名称为 mycookie
的 cookie,您可以使用此配置片段来完成:
if ($cookie_mycookie != "foobar") {
return 401;
}
来自 nginx manual 的 if
命令:
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 .. 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.
关于检查 cookie 是否存在并包含我的内容的教程数不胜数,在本例中 foobar
。
假设 mycookie 是我要设置的 cookie,我该如何执行以下操作。
if ($cookie_mycookie does not equal "foobar") {
return 401;
}
我试过以下方法都无济于事。
if (!$http_mycookie ~* "foorbar" ) {
return 401;
}
谢谢!
在 Nginx 中,每个 cookie 都可以在嵌入式变量中使用 $cookie_CookieName
。如果您想检查名称为 mycookie
的 cookie,您可以使用此配置片段来完成:
if ($cookie_mycookie != "foobar") {
return 401;
}
来自 nginx manual 的 if
命令:
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 .. 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.