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?
- To reduce server load, especially CPU works.
- To make a website load faster, because cached and stored in RAM.
- 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.
Prerequisite
- This tutorial works on Ubuntu. In this example I use My Playground server runs Ubuntu 12.04.
- You may also need to follow most common practices for initial Ubuntu server setup.
- Install Nginx on your server or read my previous guide to setup Nginx and PHP5 on 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 http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
Screenshot taken from my server:
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 http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0
It looks like this:
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
screenshot:
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:
Wait few seconds for the installation process to finish with output something like this:
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"
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:
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 = "127.0.0.1"; .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 = "127.0.0.1"; .port = "8080"; }
Now you can exit Nano editor screen.
Step 5 – Configure 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/domain.com
Obviously you have to change “domain.com” to your own domain name.
Now find following line:
listen 127.0.0.1:80;
then change it to:
listen 127.0.0.1:8080;
Or if you don’t see that line simply add it there so the updated line should look like this:
server { listen 127.0.0.1:8080;
Screenshot taken from my server:
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:
this command to restart:
service nginx restart
it gives you output like this:
Step 8 – Now restart Varnish as well:
service varnish restart
It should give you the OK message:
Done.
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.
output (in the bottom):
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:
varnishstat
It looks like this:
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.
It’s really best blog. +1 +1 …. ( -: ( -: ( -:
Hi,
I got this error:
“The default action is to keep your current version.
*** varnish (Y/I/N/O/D/Z) [default=N] ? Y
Installing new version of config file /etc/default/varnish …
* Starting HTTP accelerator varnishd [fail]
Message from VCC-compiler:
Unterminated string at
(‘input’ Line 32 Pos 19)
if (req.url ~ “^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|
——————#############################################
Running VCC-compiler failed, exit 1
VCL compilation failed
invoke-rc.d: initscript varnish, action “start” failed.
E: Sub-process /usr/bin/dpkg returned an error code (1)”
@geisha That errors tells you that there’s a problem on line 32 of /etc/default/varnish – so edit that file. You way need to comment out those lines to get it working. Put a # at the beginning of each line, then restart varnish and your web server.
Hi,
Thanks for the great post, I am running server in AWS, with ubuntu 12.04, nginx and varnish, i have configured as per the above post, but while trying to access the site with the IP, ex: 53.44.33.22/test, site is loading but url is changing as 53.44.33.22:8080/test, how to overcome this?
I cant find this /etc/nginx/sites-available/, i used Webuzo crontrol panel
Thank you, guide searched long
tell me how to configure varnish with multiple domain on the same server
404 Not Found
Hii… thanks for this useful information..
but whenevr i hit “varnishstat” command it gives me following error :
“Cannot open /usr/local/var/varnish/sumit-DL-H61M-VG4/_.vsm: No such file or directory”
i’m using nginx server on ubuntu.
Thanx in advance.
It looks like varnishlog is not pointing to the correct directory, or has not access to it. Please check the command line options of varnishd. If the deamon run with -n argument, you have to add it to varnishlog as well. See this qa.
Super helpful, thank you!
Can I buy you a coffee or a beer?
a coffee may be, not beer 🙂
Hi,
Thank you for your fantastic guide. I followed the instruction, however can’t get it working. My websites can’t load with 8080 and varnishstat is showing zero hits. What else could be wrong?
When I do:
$ sudo netstat -anp –tcp –udp | grep LISTEN
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 1959/nginx
tcp 0 0 10.0.1.110:53 0.0.0.0:* LISTEN 772/named
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 772/named
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 726/sshd
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 990/master
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN 772/named
tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 1918/varnishd
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 846/mysqld
tcp6 0 0 :::53 :::* LISTEN 772/named
tcp6 0 0 :::22 :::* LISTEN 726/sshd
tcp6 0 0 :::25 :::* LISTEN 990/master
tcp6 0 0 ::1:953 :::* LISTEN 772/named
tcp6 0 0 ::1:6082 :::* LISTEN 1918/varnishd
It seems Varnish is not listening to port 80. I’m on Ubuntu 14.04 with nginx.
double-check your /etc/default/varnish file and make sure you have proper configuration and set varnish to listen at port 80. Do not forget to restart both nginx and varnish
I tried installing same settings on another server with 64-bit Ubuntu 14.04.3 and Varnish 4.1. Seems working. Varnishstat showing Hit counts. However, centralops.net can’t detect it.
Here’s an output sample from centralops.net:
HTTP/1.1 503 Service Unavailable
Date: Thu, 03 Mar 2016 04:30:50 GMT
Content-Type: text/html; charset=UTF-8
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Retry-After: 3600
Age: 0
x-Cache: uncached
Connection: close
Is my Varnish actually working?
Problem solved!
It’s actually working. Somehow the code I copied somewhere had the headers removed in sub vcl_deliver {}
Finally all set and working. Cheers!