skip to Main Content

I am having an issue with Varnish installed with Magento 2, I have followed this tutorial https://slack-redir.net/link?url=https%3A%2F%2Fwww.techrepublic.com%2Farticle%2Fhow-to-speed-up-apache-with-varnish-http-cache%2F

I used the next command :

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

I normally see in examples that it should have a HIT but I cannot see it, maybes it is badly configured, also when I click a link on my site it will take 6s, then I click on same link and same amount of time so this means it might not be working

My files:

File /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.
}

File 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

File /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 file

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

4

Answers


  1. It is not a cache hit.

    You can see if cache was hit by checking value of X-Varnish: 131250. Single number is a cache miss, two numbers, e.g. X-Varnish: 131250 12345 is a cache hit.

    You need to download special Magento VCL file from Magento admin, not use the default one.

    Login or Signup to reply.
  2. You can identify cache HIT or MISS by adding some code in sub vcl_deliver section of /etc/varnish/default.vcl.

    if (obj.hits > 0) {
        set resp.http.Cache-Tags = "HIT";
    }
    else {
        set resp.http.Cache-Tags = "MISS";
    }
    
    1. add the above code in vcl_deliver of /etc/varnish/default.vcl.
    2. reload/restart varnish.
    3. Curl -I your request
    4. Look for Cache-Tags in response header.

    Method 2:

    You can issue a command in varnish server and check the requests are hit or miss

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

    If you issue above command and access the website it will show you it’s a miss or hit in the command line.

    Login or Signup to reply.
  3. Once you are done with your Varnish setup, you can verify varnish hit by the following method:

    Go to your Magento front-end -> inspect the page by right-clicking mouse -> go to network tab at inspect -> clear the inspect console.

    Reload the page URL once again by selecting the URL of Magento, and you will see the details like cache hit.

    screenshot

    Login or Signup to reply.
  4. X-Varnish: 131250

    If its a hit then you will find two values.
    One of the value will the the values associated with previous request that was cached and used to deliver cache this time.

    A single after X-Varnish means this is a miss.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search