Optimize Magento 1.9 with SSL, Nginx and Varnish Cache

Updated 14 June 2017

Magento store is slow!

Well, if you are running your store over magento framework, you might have the very same issue. The problem resides in the fact that your website isn’t properly optimized. The point is, that setting up an architecture for your store isn’t a big deal, tuning it properly is the key to desired performance.

Talking about optimization, a website is properly optimized or not depends upon the way the server responds, how data is being fetched and delivered, server’s load handling capacity, etc. To improve the performance and reduce the load on backend server, a frontend server acting as reverse proxy server can be set-up to perform the caching.

Varnish acts as a http accelerator and a reverse proxy caching server. Varnish Cache visits your server once to cache the page, then all future requests for the same page will be served by Varnish Cache. If it doesn’t have a request cached, it will forward the request to your backend and then cache its output.

Magento 2.x is fully compatible with Varnish cache and it generates Varnish configuration file which can be applied directly without any changes. Magento 1.x, however, isn’t fully compatible with Varnish cache. By default, Varnish doesn’t cache requests with cookies and Magento sends the frontend cookie with every request causing a (near) zero hit-rate for Varnish’s cache. Magento 1.x uses Turpentine which is a full page cache extension for Magento that works with Varnish.

Also, in order to make our store fully secure, SSL should be enabled. In our case, as Varnish runs over HTTP and it doesn’t understand HTTPS requests, we have to set up Nginx that runs over port 443 (allowing HTTPS requests) and pass the requests to the Varnish server. Then Varnish fetch data from the backend as per requests and also keeps a copy of fetched data in order to deliver it when the same requests are made in future.

In addition to this, you might encounter sessions and cookies related issues with Turpentine running along with SSL. I have added some additional files in order to fix this bug.

So, our architecture comprises of :

  • Apache acting as a backend server running over port 8080.
  • Varnish acting as a reverse proxy caching server running over port 6081.
  • Nginx acting as a frontend server, providing SSL termination, running over port 443.

Let’s begin with server installation and configuration.

Update and Install LAMP server.

It will install apache2 server, php 5 and mysql server. Check their version.

Create Database.

Install necessary PHP extensions.

For Magento to work properly, adjust PHP memory limit as,

Change

To

Now, Install magento, unzip the file and move folder to /var/www/ directory.

Setup Virtual Host.

Enable rewrite module.

Change ownerships.

Create the host entry.

Add,

Now hit the URL http://magento.example.com, and configure Magento.

Now, for reverse proxy caching, install Varnish server.

check the varnish version.

configure varnish.

and do the following settings for varnish 3.x.

To make Magento 1.9 work with Varnish cache, install a magento extension, Turpentine.

Install and configure Turpentine.

http://www.magentocommerce.com/magento-connect/turpentine-varnish-cache.html

Sign in with your log in credentials and get the extension key.

Go to admin panel and configure the following settings.

System > Magento Connect > Magento Connect Manager > admin details Login >

  • Paste extension key to install
  • Refresh the page by clicking bottom link (Refresh) after installation.
  • Click Return to admin.
  • Logout & Re-login.

Now, Go to,

System > Cache Storage Management >

Enable following cache.

  • Varnish Pages
  • Varnish ESI Blocks

Disable all the other cache.

System > Configuration > TURPENTINE > varnish option :

  • Server : Varnish Version -> Auto
  • Server List : 127.0.0.1:6082
  • Config File Location : {{root_dir}}/var/default.vcl
  • Custom VCL File Location : {{root_dir}}/app/code/community/Nexcessnet/Turpentine/misc/custom_include.vcl

Now,

copy the key and paste it in ‘Varnish Authentication Key’ with ‘\n’ in the end.

And set the rest settings to default settings.

Now go to TURPENTINE > Caching Options

  • Backend Host : 127.0.0.1
  • Backend Port : 8080

Other setting should be configured as per requirement
Save Configuration.

Goto Admin > System > Cache Storage Management.

Click on

  • Flush Magento Cache.
  • Flush Cache Storage.
  • Apply Varnish Config.
  • Save Varnish Config.

Here, Turpentine will generate a VCL file as per our Varnish version. Take backup of default.vcl file and create a symlink of the file generated by Turpentine.

Varnish runs over HTTP and it doesn’t support SSL. To enable SSL, install and configure Nginx in front of the Varnish.

Generate keys and certificate for SSL.

Now do the following settings.

Change the ports as,

and edit the file as,

Change listening ports in backend server configuration file as,

Now, configure Nginx for enabling SSL.

Check Nginx configuration.

Setup the proxy parameters.

Adjust apache settings.

Add,

Check Apache configuration.

Now go to admin panel, and

System > Configuration > Web > Secure >

Change the settings as,

  • Base Link URL : https://magento.example.com/
  • Use Secure URLs in Frontend : YES
  • Use Secure URLs inAdmin : YES
  • Offloader header : HTTP_SSL_OFFLOADED

Now to solve sessions related issues for Turpentine when run over ssl, add the following to magento root directory.

Clear all the sessions and cookies.

Restart apache, nginx and varnish to enable all the settings.

Hit the URL  https://magento.example.com/

To check cache hits use Varnish tools.

Also, server response can be seen by,

Additionally, to configure Magento with Redis as backend cache, CLICK HERE.

                                                       

IN CASE OF ANY QUERY, CONTACT US

Category(s) magento
author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


6 comments

  • riaxin.com
  • PeepingTom
    • Alankrit Srivastava (Moderator)
  • Configure SSL in magento 2 - Cloudkul
  • Dale
  • Comparing Varnish and Nginx as Reverse Proxy for Magento 1.9 Store - Cloudkul
  • Start a Project






      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home