【问题标题】:Is this a varnish hit?这是清漆的打击吗?
【发布时间】:2019-03-21 13:01:31
【问题描述】:

我在使用 Magento 2 安装 Varnish 时遇到问题,我已按照本教程进行操作 https://slack-redir.net/link?url=https%3A%2F%2Fwww.techrepublic.com%2Farticle%2Fhow-to-speed-up-apache-with-varnish-http-cache%2F

我使用了下一个命令:

curl -I localhost/folder1/folder2
HTTP/1.1 200 OK
Date: Tue, 16 Oct 2018 22:23:50 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Fri, 16 Jun 2017 15:47:07 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 131250
Age: 0
Via: 1.1 varnish-v4
ETag: W/"2c39-55215b29954a5-gzip"
Accept-Ranges: bytes
Connection: keep-alive

我通常在示例中看到它应该有一个 HIT 但我看不到它,也许它配置错误,当我点击我网站上的链接时,它需要 6 秒,然后我点击相同的链接和相同数量的时间,所以这意味着它可能无法正常工作

我的文件:

文件 /etc/varnish/default.vcl

#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
}

文件 000-default.conf:

etc/apache2/sites-available/000-default.conf
<VirtualHost *:8080>
blablablabl
</VirtualHost>

/etc/default/varnish
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"
VARNISH_LISTEN_PORT=80

文件 /lib/systemd/system/varnish.service

[Unit]
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd

[Service]
Type=simple
LimitNOFILE=131072
LimitMEMLOCK=82000
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
ExecReload=/usr/share/varnish/reload-vcl
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true

[Install]
WantedBy=multi-user.target

port.conf 文件

/etc/apache2/ports.conf
Listen 8080
#blablabla

【问题讨论】:

    标签: magento2 varnish varnish-vcl


    【解决方案1】:

    这不是缓存命中。

    您可以通过检查X-Varnish: 131250 的值来查看缓存是否被命中。单个数字是缓存未命中,两个数字,例如X-Varnish: 131250 12345 是缓存命中。

    您需要从 Magento admin 下载特殊的 Magento VCL 文件,不要使用默认的。

    【讨论】:

      【解决方案2】:

      您可以通过在 /etc/varnish/default.vcl 的子 vcl_deliver 部分添加一些代码来识别缓存 HIT 或 MISS。

      if (obj.hits > 0) {
          set resp.http.Cache-Tags = "HIT";
      }
      else {
          set resp.http.Cache-Tags = "MISS";
      }
      
      1. 在/etc/varnish/default.vcl的vcl_deliver中添加上述代码。
      2. 重新加载/重新启动清漆。
      3. 卷曲 - 我是你的要求
      4. 在响应标头中查找 Cache-Tags。

      方法二:

      您可以在 varnish 服务器中发出命令并检查请求是否命中或未命中

      sudo varnishncsa -F '%U%q %{Varnish:hitmiss}x'

      如果您发出上述命令并访问该网站,它将在命令行中向您显示这是未命中或命中。

      【讨论】:

      • 把这个留在这里:info.varnish-software.com/blog/using-obj-hits 显然不值得单独回答,但我觉得我必须尽可能多地与这个 sn-p 作斗争:-)
      • @GuillaumeQuintard 谢谢兄弟。这是正确的方法,解释得很好。
      【解决方案3】:

      完成清漆设置后,您可以通过以下方法验证清漆命中:

      转到您的 Magento 前端 -> 通过右键单击鼠标检查页面 -> 在检查时转到网络选项卡 -> 清除检查控制台。

      选择 Magento 的 URL 再次重新加载页面 URL,您将看到缓存命中等详细信息。

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      • @Khaleel 链接的文本是答案的重要部分。如果链接断开,答案仍然可用。
      【解决方案4】:

      X-清漆:131250

      如果它成功了,那么你会发现两个值。 其中一个值将与上一个请求相关联的值被缓存并用于这次传递缓存。

      X-Varnish 之后的单曲意味着这是一个失误。

      【讨论】:

        猜你喜欢
        • 2012-09-23
        • 2014-07-11
        • 1970-01-01
        • 2012-07-10
        • 1970-01-01
        • 1970-01-01
        • 2018-03-01
        • 1970-01-01
        • 2016-12-25
        相关资源
        最近更新 更多