如何在nginx中从cookie中提取一些值

我是Nginx的新手,希望能得到一些帮助。

我想从nginx的浏览器cookie中提取某些数据(由我的PHP脚本设置的某些字段),以便我可以logging它。 如果可能的话,我想通过修改nginxconfiguration来做到这一点。

任何指针/帮助将不胜感激。

您可以使用$cookie_COOKIE_NAME_GOES_HERE变量来访问cookie值。

请参阅Nginx文档

以下是一个提取HttpOnly cookie并将其作为OAuth承载标记传递给REST风险API的示例:

 http { map $http_cookie $auth_header { default ""; "~*OAuth.AccessToken=(?<token>.+)" "Bearer $token"; } server { listen 443 ssl; ssl_certificate /etc/nginx/certs/nginx.crt; ssl_certificate_key /etc/nginx/certs/nginx.key; proxy_set_header Authorization $auth_header; location / { proxy_pass https://rest-api-host.domain.com/; } } } 

如果有人在响应中使用以前的答案与几个不同的cookie正确的正则表达式是:

 map $http_cookie $auth_header { default ""; "~*OAuth.AccessToken=(?<token>[^;]+)" "Bearer $token"; } 

或更一般的用法:

 map $http_cookie $auth_header { default ""; "~*yourCookieName=(?<variable>[^;]+)" "the value you wanna set $variable"; }