加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

检查cookie是否包含指定内容NGINX

发布时间:2020-12-14 18:32:20 所属栏目:资源 来源:网络整理
导读:有无数的教程可以检查cookie是否存在并包含我的内容,在本例中为foobar. 假设mycookie是我想要设置的cookie,我该如何做以下操作. if ($cookie_mycookie does not equal "foobar") { return 401;} 我试过以下无济于事. if (!$http_mycookie ~* "foorbar" ) { r
有无数的教程可以检查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 for 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读