73K Views

Optimize Magento 1.9 with SSL, Nginx and Varnish Cache

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
. . .

Comments (6)

Add Your Comment

  • Comparing Varnish and Nginx as Reverse Proxy for Magento 1.9 Store - Cloudkul
    […] turpentine (a full page cache extension for magento), it significantly improves the cache hit rate. Optimisation of magento 1.9 with Varnish and Turpentine can deliver better […]
  • Dale
    Thanks for this. It’s a big help but did you guys ever have issues with headers and footer blocks disappearing?
    And in place of those blocks it would say ESI processing not enabled?

    Here’s my config.
    DAEMON_OPTS=”-a :80 \
    -T localhost:6082 \
    -f /etc/varnish/default.vcl \
    -S /etc/varnish/secret \
    -p cli_buffer=16384 \
    -p esi_syntax=0x2 \
    -s malloc,1g”

  • Configure SSL in magento 2 - Cloudkul
    […]       Read More: Optimize Magento 1.9 with SSL, Nginx and Varnish Cache […]
  • PeepingTom
    My Nginx server is not on localhost but on another server in the local area network. Are there any changes in configuration? I’m using Magento CE 1.9.3.3, Varnish 4.0.2 and Nginx 1.13.1 running on both debian 8. It keep redirecting on loop from https to http and viceversa.
    • Alankrit Srivastava
      Can you post the nginx configuration you are using in your “/etc/nginx/sites-enabled/default” file?
  • riaxin.com
    That’s it. You have successfully installed Magento 2 with Redis as a session storage and page caching, Varnish as a full page caching and Nginx as SSL termination on your Ubuntu 16.
  • css.php