Friday, October 3, 2014

My Attempt of Installing Varnish with Nginx + WP Backend in Ubuntu 14.04

This is my attempt of installing Varnish before a Nginx web server hosting multiple Wordpress sites in Ubuntu 14.04.

Prerequisites

Before the installation, the server already had:
Ubuntu 14.04 with Nginx and Wordpress installed and configured.


First Step

The Varnish package can be obtained in repo.varnish-cache.org/ubuntu/

This is what I do to get the source:

$ sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

Then, I added the Varnish package into list of sources:

$ sudo nano /etc/apt/sources.list

Inside the file, I added this line:

deb http://repo.varnish-cache.org/ubuntu/ trusty varnish-3.0

Then, I install them:

$ sudo apt-get update
$ sudo apt-get install varnish libvarnish-dev


Step two - Configure Varnish

Now, I configure Varnish to listen to port 80 (web):

$ sudo nano /etc/default/varnish

I find this block of configurations and modify it to listen to port 80:

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

Then, I modify the default.vcl to look for webserver content:

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

backend mysite2 {
    .host = "127.0.0.1";
    .port = "8800";
}

backend mysite3 {
    .host = "127.0.0.1";
    .port = "4000";
}

sub vcl_recv {
        if (req.http.host ~ "mycoolwebsite1.com") {
                set req.backend = mysite2;
        } else if (req.http.host ~ "notthatcoolwebsite.com") {
                set req.backend = mysite3;
        } else {
                set req.backend = default;
        }

        if (!(req.url ~ "wp-(login|admin)")) {
                unset req.http.cookie;
        }
}

sub vcl_fetch {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;
        }
}

My Varnish installation is configured to listen to backend in the localhost with port 8080, 8800 and 4000 respectively.

The requests will be redirected accordingly, note that Wordpress cookies in login and admin are not cached.

Then, I configure my Nginx webserver for it to work correctly.


Step Three - Configure Nginx

I have already have a Nginx webserver running, now I have to configure it.

Firstly, I edit the vhost file:

$ sudo nano /etc/nginx/sites-enabled/blog-farms

I added this to my server blocks:

server {
    # listen [::]:80 ipv6only=off;
    listen 127.0.0.1:8080 ipv6only=off;
    server_name mainwebsite.com *.mainwebsite.com;

 ... blah blah blah ...

Then, I added configurations for other backend as well... :)

server {
    # listen [::]:80 ipv6only=off;
    listen 127.0.0.1:8800 ipv6only=off;
    server_name mycoolwebsite1.com *.mycoolwebsite1.com;

 ... another server config ...

Another one:

server {
    # listen [::]:80 ipv6only=off;
    listen 127.0.0.1:4000 ipv6only=off;
    server_name notthatcoolwebsite.com *.notthatcoolwebsite.com;

 ... yet another server config ...

Then, I restart all 'da servers!

$ sudo service nginx configtest
$ sudo service nginx restart
$ sudo service varnish restart

Now it is up and running!

I can check the Varnish stats by typing this:

$ varnishstat

This will show:

$ varnishstat
0+00:22:49
Hitrate ratio:        1        1        1
Hitrate avg:     0.0000   0.0000   0.0000

           1         0.00         0.00 client_conn - Client connections accepted
           1         0.00         0.00 client_req - Client requests received
           1         0.00         0.00 cache_miss - Cache misses
           1         0.00         0.00 backend_conn - Backend conn. success
           1         0.00         0.00 backend_recycle - Backend conn. recycles
           1         0.00         0.00 fetch_chunked - Fetch chunked

... a long long page ( ◕ ω ◕ ) ...

That's it!

Thanks for reading! (If there is any visitor around.)

No comments:

Post a Comment