gpt4 book ai didi

nginx - 检查cookie是否不包含指定的内容NGINX

转载 作者:行者123 更新时间:2023-12-04 10:17:03 26 4
gpt4 key购买 nike

关于检查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 $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.

关于nginx - 检查cookie是否不包含指定的内容NGINX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31074618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com