有无数的教程可以检查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; }
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.