How To Configure Varnish Cache In Magento With Ubuntu

Varnish Cache is one of the fastest known cache technology.. The technical principle of Varnish is that a user hits the Varnish server first before the request gets redirected to the web server if no cache version is available. If Varnish has found a cached version of the requested URL , the page will be delivered directly from the ram which takes only some milliseconds. The response time will be around 35 milliseconds + latency.

Step one : Installation of Varnish Server On Ubuntu 12.04 :
The varnish site recommends installing the varnish package through their repository.
Start the process by grabbing the repository:
*(Keep in mind that you are a root user while executing the below commands and if you are not root then use sudo in all the lines)

1.curl | sudo apt-key add - /etc/apt/sources.list
Inside the file, add varnish repository to the list of sources.
deb lucid varnish-3.0
3. Finally, update apt-get and install varnish.
apt-get update
apt-get install varnish

Step Two : Configure Varnish

Once you have both nginx and varnish installed, you can start to configure them to ease the load on your virtual private server.
Varnish will serve the content on port 80, while fetching it from nginx which will run on port 8080.

1. Edit /etc/default/varnish file.

Find the lines under “DAEMON_OPTS”— in the Alternative 2 section, and change the port number by “-a” to 80. The configuration should match the following code:

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

Save and exit.

Step Three : Configuring Nginx
Edit the vhost file as shown below
vi /etc/nginx/sites-available/your.vhost
## listen for ipv4;
this line is default and implied [...]


Step Four : Restart Nginx and varnish.
service nginx restart
service varnish restart

Then check your browser.
If its still not working,try below commands

service nginx stop
service varnish restart
service nginx start

Check varnish by following commands.

Pheeww! Sit back,relax and check your website’s performance.