AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 828452
Accepted
Bogdan
Bogdan
Asked: 2017-01-25 22:13:48 +0800 CST2017-01-25 22:13:48 +0800 CST 2017-01-25 22:13:48 +0800 CST

使用“Set-Cookie”标头清漆缓存响应

  • 772

我有一个页面,它根据 URL 发送类似语言的“Set-Cookie”标头。问题是这个页面会获得相当高的命中率,但现在还不能去掉“Set-Cookie”标题以便 Varnish 4 缓存它。

该文档仅显示如何unset beresp.http.set-cookie或我可以将 cookie 添加到带有hash_data(req.http.cookie). 据我所知,将 cookie 添加到哈希仅适用于请求 Cookie,而不适用于后端设置的 cookie。

我很确定vmod_header可能是解决方案的一部分,但我将如何使用它来缓存我匹配的 cookieberesp.http.Set-Cookie == "language=en_us; path=/; domain=mydomain.tld"并缓存此响应?

http varnish cookies
  • 2 2 个回答
  • 8747 Views

2 个回答

  • Voted
  1. Benjamin Baumann
    2017-01-26T07:04:55+08:002017-01-26T07:04:55+08:00

    缓存键(也就是在 hash_data 中计算的哈希)是在接收请求时创建的。尚未收到来自服务器的响应。之后您无法更改它。

    我可以看到的两个选项是:

    • 要么禁用带有 Set-Cookie 标头的响应缓存(我认为这可能是由清漆的 defautl 完成的)
    • 或列出您在 Cookie 计算中使用的传入请求中的参数(url、主机、接受语言...),并使用 hash_data 将它们添加到请求哈希中
    • 1
  2. Best Answer
    Bogdan
    2017-04-21T02:00:35+08:002017-04-21T02:00:35+08:00

    我最终解决了这样的问题:

    ...
    # The data on which the hashing will take place
    sub vcl_hash {
      hash_data(req.url);
      if (req.http.host) {
            hash_data(req.http.host);
      } else {
            hash_data(server.ip);
      }
    
      # If the client supports compression, keep that in a different cache
      if (req.http.Accept-Encoding) {
            hash_data(req.http.Accept-Encoding);
      }
    
      # We add the cookie in the mix with the hash because we actually set cr_layout
      # It should be OK in this particular instance if the number of cookies will not grow
      hash_data(req.http.Cookie);
      return (lookup);
    }
    
    # This function is used when a request is sent by our backend
    sub vcl_backend_response {
      # If we find our layout cookie we copy it to a header and then we remove it so we can cache the page
      # Later after we deliver the page from cache we set the cookie from the header and then remove that header
      if ( beresp.http.Set-Cookie && beresp.http.Set-Cookie ~ "cr_layout" ) {
        set beresp.http.first-visit = beresp.http.Set-Cookie;
        unset beresp.http.Set-Cookie;
      }
    }
    
    sub vcl_deliver {
      # We found that we created a header earlier to remove the cr_layout cookie temporary
      # Now we create that cookie again and remove the header.
      if (resp.http.first-visit) {
        set resp.http.Set-Cookie = resp.http.first-visit;
        unset resp.http.first-visit;
      }
    }
    
    • -1

相关问题

  • 设置 http 代理以使用 Web 界面?

  • 无法连接到 Ubuntu Desktop 中的端口 80 或 VMWare Workstation 6.52 中的 Server 9.04

  • IIS 7.5 (Windows 7) - HTTP 错误 401.3 - 未经授权

  • 为什么有些网站的网址中没有“www”就无法显示?[关闭]

  • Tomcat 6 HTTP 日志滚动和清除

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve