【问题标题】:How to read cookie in nginx如何在 nginx 中读取 cookie
【发布时间】:2015-11-25 12:36:46
【问题描述】:

我是 UI 编程和 nginx 方面的新手。对不起,如果问题很愚蠢。此外,我尝试了与此类似的 stackoverflow 问题中存在的几乎所有答案,但没有任何帮助我解决此问题

我需要做什么:

检查是否存在名为“AUTH_COOKIE”的 cookie。如果其当前 proxy_pass 到 site1。否则 proxy_pass 站点 2。为此,我需要阅读 cookie。

我尝试了什么:

我尝试将 cookie 读取为 $cookie_AUTH_COOKIE,但它没有为我提供任何结果。我尝试使用 $http_cookie 阅读。那也没用

我尝试遵循此处的要点https://gist.github.com/rnorth/2031652https://gist.github.com/NickSto/6920790。但是没用。

所以现在我只是尝试读取 cookie 值并使用 echo 返回它。响应不包含我的 cookie 的值。

这是我使用 echo 所做的 nginx 配置

server {
    listen       3001;
    server_name  <my_host>;

    # Load configuration files for the default server block.
    # include /etc/nginx/default.d/*.conf;

    # set $auth_cookie "check";
    if ($http_cookie ~* "AUTH_COOKIE=([a-z0-9]+)(?:;|$)") {
        set $auth_cookie $1;
    }
    location / {
        resolver 8.8.8.8;
        echo "This is a test from $host with $auth_cookie. Cookie $cookie_AUTH_COOKIE. Referer $http_referer";
    }
}

当我通过 advanced rest client 调用它时,我得到的响应为

This is a test from localhost with . Cookie . Referer

可以看到 $auth_cookie、$cookie_AUTH_COOKIE 和 $http_referer 没有返回。

这是我的其他客户端的屏幕截图

我在这里错过了什么。我需要导入任何模块来读取cookie吗?

【问题讨论】:

  • 您的屏幕截图中的请求标头中没有 cookie
  • AUTH_COOKIE=test。这应该是饼干。或者有没有其他方式发送cookie
  • 可能您无法从浏览器设置任意标题。尝试使用控制台工具,例如 curl。检查此命令curl -v -H 'Cookie: AUTH_COOKIE=test' http://myhost:3001
  • 非常感谢。现在使用 [邮递员拦截器] (chrome.google.com/webstore/detail/postman-interceptor/…) 我可以传递 cookie
  • 你能用上面的cmets添加一个答案吗?

标签: cookies nginx proxy proxypass httpcookie


【解决方案1】:

您的屏幕截图上的请求标头中没有 cookie。

您可能无法使用此工具设置任意标题。尝试使用控制台工具,例如curlGET。检查这条命令curl -v -H 'Cookie: AUTH_COOKIE=test' http://myhost:3001

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多