When we have installed Varnish on your hosting account, you can use these tools to manage Varnish.
We have made a script which configures the basic settings to use Varnish with Magento 2. We recommend testing on a Staging account first.
#Run the script from your magento2 root folder.
~/bin/configure_varnish_magento2.sh
When the Varnish server starts or restarts, it loads the VCL file located at:
~/.config/varnish/default.vcl
If you don't use our script to configure Magento 2, make sure the file contains the right VCL. Inside this default VCL file you can also point to the Magento 1 VCL using an "include".
~/bin/reload_varnish.sh
This command will instruct Varnish to re-read the VCL config. The syntax will be checked before reloading. Varnish will keep the cached data.
# Restart via mnage
mnage service:restart varnish
# Restart Script
~/bin/restart_varnish.sh
This command will stop and start the Varnish daemon. The cache will become empty, the VCL config will be read again. If there is a fault in your VCL, Varnish will not start after stopping.
Note: You should only need/use this command when you are debugging and it should never be used during high traffic.
You should also not add this command to your crontab. Please talk to us if you need to do it in a cron job.
The matching URL will be purged from cache on all domains.
varnishadm ban req.url '~' '^/robots\\.txt$'
The homepage of all domains will be purged.
varnishadm ban req.url '~' '^/?$'
All pages on a specific domain will be purged.
varnishadm ban req.http.host '~' '^(www\\.)?yourdomain\\.com$'
This will create a list of the URLs that caused the most cache misses the last hour.
varnishtop -p 3600 -i BereqURL
Press Q to quit.
Using Varnishstat, you can monitor your site's hit rate. For high traffic environments, this is preferably around 90% (with a minimum of 80%). Sometimes, implementing new features, might impact the hit rate. Below are the most common issues and how to debug them.
1.1 Recognizing patterns
As always, logging is key when investigating issues. Varnishlog is an easy-to-use tool and contains vital information. If you have no idea what pages are causing the low hit rate, you can start by filtering by misses.
varnishlog -q 'VCL_CALL eq MISS' -q 'VCL_CALL eq HITMISS'
When you have recognized a pattern (like the same page or the same type of pages appearing a lot), move to step 1.2. When there is no visible pattern, move to step 2.
1.2 Analyzing the patterns
When analyzing patterns, there are 2 common issues:
varnishlog -q 'ReqURL ~ "$PATH"'
Below is an example of a HASH
- Hash "https%00"
- Hash "/%00"
- Hash "varnish.m2.savvii.pro%00"
In the example above, only the URL is part of the hash. For several CMSes, it should be like this by default. However, Magento for example includes vary cookies to segment different languages and currencies. Any discrepancy in this hash (like a URL query from a marketing redirect tool), causes variation in the cache and a lower hit rate.
In a lot of cases, your CMS or certain plugins are too eager when it comes to invalidating the cache. Recognizing this issue can be done by logging bans and purges over a longer amount of time. Here are the commands we use to filter the logs.
varnishlog -q 'CLI ~ "^Rd ban"'
varnishncsa | grep PURGE
If you find the underlying issue, there are some standard solutions you could try. When having a lot of duplicate bans, you can queue your bans, delay them and make them unique. It also helps to check your timing / events. For example, Magento 2 bans a product on each purchase, before version 2.4.4. However, said product only has to be banned when the stock status changes (backorder or out of stock). It can also help to bundle item imports instead of spreading them.
You can use our CLI Tool dbug for quick access to live data and varnish stats, check out the dbug page for more information.
We have also configured the following advanced Varnish CLI tools to use your Varnish daemon out of the box.
Was dit artikel nuttig?
Dat is fantastisch!
Hartelijk dank voor uw beoordeling
Sorry dat we u niet konden helpen
Hartelijk dank voor uw beoordeling
Feedback verzonden
We stellen uw moeite op prijs en zullen proberen het artikel te verbeteren