Varnish Cache and Apache with SSL in Multi Store Environment – Setup Guide

magento2varnish

I want to install and configure varnish with Magento 2 on my server running apache as incredibly impressed with the performance gains. Just wanted to know a bit about configuring SSL with this as have not done this in my dev environment.

I have also however been told by my hosting provider that to have varnish work on my site i can only have one site on my server. Is this correct? My testing shows multiple sites seemingly working fine so wondering what this could be about?

Best Answer

Varnish in the open source version does not support SSL termination. Therefore you have to set up a reverse proxy like a nginx or similar. The nginx is responsible to terminate the SSL and forward the request to varnish.

Magento 2 works well with varnish, even in a multi-site environment.

As you use Apache, you need to enable mod_proxy. In addition to that add a virtual host that listens to port 443 like so:

#/etc/apache2/sites-available/your-site.com-ssl
<VirtualHost *:443>
    ServerName www.your-site.com

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:80/ 
    RequestHeader set X-Forwarded-Port "443"
    RequestHeader set X-Forwarded-Proto "https"

    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/example.com.crt
    SSLCertificateKeyFile /etc/apache2/ssl/example.com.key
    SSLCertificateChainFile /etc/apache2/ssl/example.com.chain
</VirtualHost>

The ProxyPass should point to your varnish instance.

Related Topic