Nginx 正则表达式检查 $http_cookie 名称的 MD5

Nginx Regex to check MD5 of $http_cookie name

所以我映射 $http_cookie 来检查客户端发送的所有 cookie,我想拦截的唯一一个获取 cookie 值的是任何具有 MD5 HASH 的 cookie。

检测 MD5 哈希的正则表达式是这个

[0-9a-f]{32}

但是当我将它添加到我的地图指令时,Nginx 不会 运行 因为正则表达式是错误的。

这是我的 cookie 映射,问题是它获取了所有 cookie,我只想要具有 MD5 总和的那些。

map $http_cookie $session_id_value {
default '';
~^.*.+\=(?<session_value>[\w]+).*$ $session_value;
}

我试试这个

map $http_cookie $session_id_value {
default '';
~^.*[0-9a-f]{32}.+\=(?<session_value>[\w]+).*$ $session_value;
}

但是 Nginx 不喜欢我的正则表达式。所以它会出错,不会 运行.

我使用 echo 模块进行测试,以查看我的正则表达式已抓取的 cookie 的值,但目前它一直在抓取第一个随机 cookie,而不是名称具有 MD5 哈希值的 cookie。

echo "Session Cookie Value : $session_id_value";
echo "httpcookie : $http_cookie";

这是语法错误。来自 rewrite documentation:

If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.

尝试:

map $http_cookie $session_id_value {
default '';
"~^.*[0-9a-f]{32}.+\=(?<session_value>[\w]+).*$" $session_value;
}