Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Varnish To Work on Magento

First please forgive me for total lack of understanding of Varnish. This is my first go at doing anything with Varnish.

I am following the example at: http://www.kalenyuk.com.ua/magento-performance-optimization-with-varnish-cache-47.html

However when I install and run this, Varnish does not seem to cache. I do get the X-Varnish header with a single number and a Via header that has a value of 1.1 varnish

I have been told (by my ISP) it is because of the following cookie that Magento sets:

Set-Cookie: frontend=6t2d2q73rv9s1kddu8ehh8hvl6; expires=Thu, 17-Feb-2011 14:29:19 GMT; path=/; domain=XX.X.XX.XX; httponly

They said that I either have to change Magento to handle this or configure Varnish to handle this. Since changing Magento is out of the question, I was wondering if someone can give me a clue as to how I would configure Varnish to handle this cookie?

like image 423
Josh Pennington Avatar asked Feb 17 '11 13:02

Josh Pennington


People also ask

How Varnish works with Magento?

Basically, Magento Varnish builds fragments files that are stored in memory. This helps Varnish eliminate the time and network bandwidth consumption for upcoming requests. While processing requests, HTTP manages all calls for CSS, HTML, JavaScript, and images. Varnish works to proxy these requests for the web server.

How can I check Varnish is working in magento2?

You can prove it through log in to Magento as a file system owner and type the following command: rm -rf <your Magento install dir>/var/page_cache/*. Then choose one of the cacheable pages and check the var/page_cache/ directory there. The directory should be empty. If you had to clear it, restart the Varnish.

How to configure varnish in Magento 2?

To configure Magento to use Varnish: Log in to the Admin as an administrator. Click Stores > Settings > Configuration > Advanced > System > Full Page Cache. From the Caching Application list, click Varnish Caching. Enter a value in the TTL for public content field. Expand Varnish Configuration and enter the following information:

What is the grace period for varnish in Magento?

The grace period determines how long Varnish serves stale content if the backend is not responsive. The default value is 300 seconds. Click Save Config. You can also activate Varnish from the command line–instead of logging in to the Admin—using the Magento command-line interface tool:

How to set up full page cache in Magento 2?

1 Log in to the Magento Admin as an administrator. 2 Click Stores > Settings > Configuration > Advanced > System > Full Page Cache. 3 From the Caching Application list, click Varnish Caching. 4 Enter a value in the TTL for public content field. More items...

How do I configure Varnish caching?

Click Stores > Settings > Configuration > Advanced > System > Full Page Cache. From the Caching Application list, click Varnish Caching. Enter a value in the TTL for public content field. Expand Varnish Configuration and enter the following information:


2 Answers

http://moprea.ro/2011/may/6/magento-performance-optimization-varnish-cache-3/ describes the Magento extension that enables full page cache with varnish. This extension relies on Varnish config published on github.

These are the features already implemented:

  1. Workable varnish config
  2. Enable full page caching using Varnish, a super fast caching HTTP reverse proxy.
  3. Varnish servers are configurable in Admin, under System / Configuration / General - Varnish Options
  4. Automatically clears (only) cached pages when products, categories and CMS pages are saved.
  5. Adds a new cache type in Magento Admin, under System / Cache Management and offers the possibility to deactivate the cache and refresh the cache.
  6. Notifies Admin users when a category navigation is saved and Varnish cache needs to be refreshed so that the menu will be updated for all pages.
  7. Turns off varnish cache automatically for users that have products in the cart or are logged in, etc.)
  8. Default varnish configuration offered so that the module is workable. Screen shots: https://github.com/madalinoprea/magneto-varnish/wiki
like image 95
Mario Avatar answered Oct 29 '22 00:10

Mario


How to cache Magento in Varnish (Theory) - There are 2 components to this

1) Static assets (eg. Images, CSS, JS) - This is a simple common pattern that involves detecting requests that belong to this category and setting a cache time (Or relying on the cache time being sent by the origin server) Example of this in gist form

2) HTML documents - This is the much more complex part of a good Magento solution. Its critical to cache HTML documents in Varnish to be able to improve Magento performance. HTML document generation is the most expensive (time consuming) thing that a Magento server will do.

The challenge with caching HTML documents comes from personalised content.

Personalised content and HTML documents

Magento, and all other ecommerce sites, manage the state of a particular user though a session. A session is a record of that particular user’s status on your site. This covers things such as: “Hello Bob” - at the top of the page “4 Things in Your Cart” - the status of your shopping cart on each page

These are items that cannot be shared amongst users and would cause a major problem should this happen (we call this “session leakage”).

How do we cache HTML pages if the HTML pages contain personalised information about who the person is and what is in their shopping cart?

There are 2 main methods of achieving this: Loading personalised elements of the page via additional requests after the page has loaded A common implementation method here is to use AJAX to request page elements that are personalised Leveraging a technology to mark components of the HTML document as cacheable and other’s uncachable (or un shareable amongst users). Varnish supports a technology called ESI (Edge Side Includes) that allows different parts of a HTML document to be cached differently.

Your Varnish implementation strategy must factor in user personalisation.

Implementation options for Varnish

Magento 1.X - The most widely used method for caching HTML documents in Magento version 1 is the open source product called Magento Turpentine (by Nexus). This is a plugin that is installed (via Magento Connect) and will automatically add ESI tags to your HTML documents so that Varnish can cache these resources. Magento Turpentine install / guide

Magento 2.X - The latest version of Magento (currently in beta) supports Varnish as its recommended solution for HTML caching in production. This is great news, Varnish is the recommended option from Magento and will work out of the box to improve your site speed.

How to make Varnish and Magento work well

Deployment is one thing, The next steps once you have a Varnish Magento solution implemented and working is to understand how well its performing. Getting metrics on cache hit rates and detailed logs on a request by request basis can be a challenge as it involves deploying a range of additional infrastructure (or being stuck doing manual log collection on a one off basis).

We have recently built this infrastructure to run Varnish as a service in the cloud (with full logs/metrics) - www.section.io - Plug aside this can be the most important element to actual success with you Varnish and Magento project as you need to be constantly tuning your implementation to manage things like varying query strings in urls (Hello google analytics "gclid"!) which can reduce your cache hit rates dramatically

like image 44
mattnthat Avatar answered Oct 29 '22 00:10

mattnthat