我在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,但是我看不到它,也许它配置不当,当我单击站点上的链接时,它会花费6s,然后我单击相同的链接,并且点击量相同时间,所以这意味着它可能无法正常工作
我的文件:
文件/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
porst.conf文件
/etc/apache2/ports.conf
Listen 8080
#blablabla
答案 0 :(得分:0)
这不是缓存命中。
您可以通过检查X-Varnish: 131250
的值来查看缓存是否被命中。单个数字是高速缓存未命中,两个数字是例如X-Varnish: 131250 12345
是缓存命中。
您需要从Magento管理员那里下载特殊的Magento VCL文件,而不要使用默认文件。
答案 1 :(得分:0)
您可以通过在/etc/varnish/default.vcl的sub vcl_deliver部分中添加一些代码来标识高速缓存HIT或MISS。
if (obj.hits > 0) {
set resp.http.Cache-Tags = "HIT";
}
else {
set resp.http.Cache-Tags = "MISS";
}
方法2:
您可以在清漆服务器中发出命令并检查请求是否命中
sudo varnishncsa -F'%U%q%{Varnish:hitmiss} x'
如果您发出上述命令并访问网站,它将在命令行中显示您是未命中或命中。