Recommended VPS Complete list of best VPS hosting providers.

Install Varnish 3 To Run With Nginx On Ubuntu Server

Complete guide how to install Varnish Cache 3 HTTP accelerator and configure it to run with Nginx on Ubuntu server. But before you read much further, if I may, I suggest you to use Nginx itself without Varnish. Why? Because without the help of Varnish, Nginx is already pretty good at handling static files. But however the decision is up to you because some users still prefer to put Varnish in front of Nginx as they believe it can increase server performance.

Few Words About Varnish

Both Nginx and Varnish Cache are having purpose to boost a server performance as well as speed the web load time. But however both are different to each other. Nginx is a web server just like Apache but it is faster while Varnish is a HTTP accelerator so it can also run side by side with either Nginx or Apache. I’ve posted the step by step tutorial about how to install, setup and configure Varnish in front of Apache on Ubuntu server. Remember that each time I say “server” it refers to both VPS or Dedicated server.

Why use Varnish?

  1. To reduce server load, especially CPU works.
  2. To make a website load faster, because cached and stored in RAM.
  3. To gain more visitors to your website.

Read more about Varnish at WikiPedia to gain more information and to understand how exactly Varnish works in boosting your server.


Setup and Configure Varnish with Nginx

Step 1 – Login to your server via SSH. You can use Terminal in Mac or Linux but as I’m using Windows, Putty is the perfect tool. You can download Putty and read how to use it. You have to login with root or sudo privilege. If you disabled root login (following my previous guide) then once logged in simply type “su” (without quotes), hit Enter then type the password of your root.

Step 2 – Ubuntu is coming with Varnish package already but it might be outdated. The solution is to add official Varnish repository url to your server to fetch newer version. Follow the steps below:

Type this command syntax:

curl | sudo apt-key add -

Screenshot taken from my server:

varnish repo

Now edit “source/list” file then add the repo url there. Use this command to edit the file:

nano /etc/apt/sources.list

then add this line there:

deb lucid varnish-3.0

It looks like this:

varnish add repo

Once done hit Control+O to save then Control+X to exit Nano editor. Now issue command syntax below to update then install Varnish:

apt-get update


apt get update

Then issue this, the main command syntax to install Varnsih:

apt-get install varnish

It you’ve been asked to continue, simply type Y then hit Enter:

apt-get install varnish

Wait few seconds for the installation process to finish with output something like this:

installed varnish

Step 3 – This is how to setup Varnish. So you’ve installed Varnish on your server and it runs already but it is not running properly, yet. You have to follow other few steps to configure Varnish so your server will be faster than before it your visitors will feel how fast it loads. What are you gonna do? All you have to do is making Varnish to run on port 80 while Nginx on port 8080. First configuration file you need to edit is located at “/etc/default/varnish”. Use Nano to edit it:

nano /etc/default/varnish

Now look for these lines there under “Alternative 2” section:

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

edit varnish config

Found it? Now edit those lines to match following to make it works in port 80:

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

It looks like this:

edit varnish port 80

Once done hit Control+O to save then Control+X to exit.

That configuration tells Varnish to use 256mb of your allocated RAM to store all cache files generated by the service. Please edit it to match your server specs. Edit that value to lower number If you are on a VPS with low amount of RAM. But you may also allocate x gb of your RAM if you are on Dedicated server.

Step 4 – Now you’ll also need to specify the backend server or in other words to tell Varnish where to look for the webserver content (in this case is to fetch from Nginx on port 8080 which you’ll also configure it later in next step below). Simply edit the “default.vcl” file:

nano /etc/varnish/default.vcl

Now you’ll see some lines like below. If not, edit it to match as following:

backend default {
    .host = "";
    .port = "8080";

# Drop any cookies sent to WordPress.
sub vcl_recv {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset req.http.cookie;

# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;

The config above says that Nginx as the backend server is available on localhost at port 8080 and Varnish will run in front of it listening on port 80 that previously used by Nginx. Pay attention to the “# Drop any cookies sent to WordPress” and “# Drop any cookies WordPress tries to send back to the client” section. Those two lines tell Varnish to drop all the cookies that don’t relate to the admin side of the WordPress site and to remove the cookies that make WordPress very difficult to cache. However if you don’t use WordPress so you can simply ditch those two sections and use this only:

backend default {
    .host = "";
    .port = "8080";

varnish setup file

Now you can exit Nano editor screen.

Step 5Configure Nginx to work and listen on port 8080. Without doing this step, Apache will still run on port 80 and so Varnish. So you have to also make Nginx to work on port 8080 by editing Virtual Hosts file of corresponding domain.

nano /etc/nginx/sites-available/

Obviously you have to change “” to your own domain name.

Now find following line:


then change it to:


Or if you don’t see that line simply add it there so the updated line should look like this:

server {

Screenshot taken from my server:

virtual hosts file

Once done save it by hitting Control+O on your keyboard then Control+X to exit Nano editor screen.

Step 6 – Now delete default Virtual Hosts file of Nginx located at “/etc/nginx/sites-enabled/default”. Issue this command:

rm /etc/nginx/sites-enabled/default

is it safe? Yes it is safe enough to remove the default enabled virtual host because the configuration template remains in the sites-available directory.

Step 7 – Now test Nginx config and restart the service using this command:

nginx -t

Should look like this:

test nginx config

this command to restart:

service nginx restart

it gives you output like this:

restart nginx

Step 8 – Now restart Varnish as well:

service varnish restart

It should give you the OK message:

restart varnish


Q: How to know / detect a server is running Varnish?

Now once you access your website in browser it should be served through Varnish. You can use Firebug to test response header or simply use Centralops domain scanner service and check mark the “service scan” option. You’ll see message saying: Via: 1.1 varnish.

test server response

output (in the bottom):

test varnish is running

Q: How to know Varnish performance?

You can know how Varnish performance by seeing its real time stats. You can see its Hit rate ration, Hit rate average, number of client connections accepted, and so on. Issue this command syntax:


It looks like this:

varnish stats

That’s it. Enjoy your blazing speed server. Also catch my complete guide how to build and configure Ubuntu server from zero.

p.s: You can simply install Nginx or Apache + Varnish cache easily using Tuxlite. Read my previous guide about Tuxlite.

Also do not forget to follow me on Twitter to get faster update.


Add a Comment

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

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.