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

前端之家收集整理的这篇文章主要介绍了检查cookie是否包含指定内容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 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.

原文链接:https://www.f2er.com/html/226051.html

猜你在找的HTML相关文章