trive/varnish

Magento 2 Varnish 修复模块

安装数: 38,433

依赖项: 0

建议者: 0

安全性: 0

星标: 38

关注者: 5

分支: 11

公开问题: 4

类型:magento2-module

1.0.0 2017-12-21 09:55 UTC

This package is not auto-updated.

Last update: 2024-09-16 11:38:40 UTC


README

描述

Magento 2 模块,用于修复使用 Varnish 作为缓存应用时出现的缓存标签问题和 503 错误。

安装

通过 composer 安装

composer require trive/varnish

修改您的 VCL 文件,内容如下

sub vcl_backend_response {
    ...(redacted)
    
    # Trive - add these two lines below - start
    # collect all x-magento-tags headers
    std.collect(beresp.http.X-Magento-Tags);
    # Trive - add these two lines above - end"
    
    return (deliver);
}

启用模块

bin/magento module:enable Trive_Varnish
bin/magento setup:upgrade

问题

当在 Magento 2 中使用 Varnish 作为缓存应用时,每个页面都会被标记为显示内容的缓存标签。一旦内容更新,这些标签就会用于缓存失效和清除。

例如,如果您在默认安装上打开主页,它将被标记为 store, cms_page and cms_page_1 缓存标签。

类别会被标记为所有分配给它们的产品的缓存标签。这意味着,如果您有一个包含大约 2000 个产品的单个类别的大型目录,那么在首次加载该类别时,您可能大约会有 20kB 值的缓存标签。

缓存标签会作为初始的非缓存请求中的 X-Magento-Tags 标头发送到 Varnish,这样它就可以标记 URL 上的显示内容,并在请求时清除所有出现对象的页面。

一旦 X-Magento-Tags 标头超过 8kB,默认的 http_resp_hdr_len Varnish 限制就会被触发,Varnish 会向客户返回 503 错误(后端获取失败)。

即使实施了来自 Magento 文档 的工作区,问题仍未解决,因为 Apache/nginx HTTP 服务器可能还会遇到另一个限制,因为它们 两者 都有 默认的 8kB 最大头值。

受影响的商店

如果您的商店受到影响,那么您是

  • 使用 Magento 2(任何版本)
  • 在商店中有大量产品,至少有一个锚定类别包含至少 2000 个产品
  • 使用 Varnish 作为 Magento 2 缓存应用,并使用默认的 http_resp_hdr_len 限制

如果您的商店可能受到影响,那么您是

  • 使用 Apache/nginx 并使用默认的 LimitRequestFieldSizelarge_client_header_buffers 或可能被 HTTP 标头触发的其他限制
  • 使用 nginx/HAProxy 并使用默认的头部长度或请求大小限制
  • 使用 Apache mod_proxy_fcgi

我们的解决方案

本模块通过将 X-Magento-Tags 分隔为 8kB 头块来解决上述问题。发送多个 X-Magento-Tags 头部不会触发大多数限制,也不会在 Varnish 后端获取时触发 503 或 FetchError http read error: overflow 错误。

请注意,在实施此模块后,应更新默认的 Magento 2 VCL 文件,以便在 Varnish 处理期间将多个 X-Magento-Tags 头部收集到一个中。