Friday, September 11, 2015

Nginx web server with Varnish cache

First let us understand about Nginx and Varnish:

Nginx (pronounced "engine x") is a web server with a strong focus on high concurrency, performance and low memory usage. It can also act as a reverse proxy server for HTTPHTTPSSMTPPOP3, and IMAP protocols, as well as a load balancer and an HTTP cache.
Nginx can be deployed to serve dynamic HTTP content on the network using FastCGI, SCGI handlers for scripts, WSGI application servers or Phusion Passenger module, and it can serve as a software load balancer.
Nginx uses an asynchronous event-driven approach to handling requests, instead of the Apache HTTP Server model that defaults to athreaded or process-oriented approach, where the Event MPM is required for asynchronous processing. Nginx's modular event-driven architecture can provide more predictable performance under high loads.

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture.

Install & configure
Nginx can be installed on MAC using brew.
$brew install nginx
will install Nginx for you.
Then go to /usr/local/etc/nginx/nginx.conf to configure the server and location settings. This is the simplest of the settings.
server {
        listen       8080;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
}
Start up Nginx:
bash-3.2$ cd /usr/local/Cellar/nginx/1.8.0/bin
bash-3.2$ nginx
bash-3.2$ ps -ef | grep nginx

Varnish can be installed on MAC using brew.
$brew install varnish
Configure default.vcl file for varnish at /usr/local/Cellar/varnish/4.0.3/etc/varnish
Minimum setting required is:
vcl 4.0;
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Startup Varnish with a command like:
bash-3.2$ sudo sbin/varnishd -a :6081 -T localhost:6082 -f etc/varnish/default.vcl -s malloc,512m

Here -a option is where varnish will listen for traffic.
-T is for the admin url
-f to give vcl file


Run Varnish with Nginx

With this setup, request sent to http://localhost:6081/ will hit Varnish, if the requested resource is cached then varnish will return it with HTTP 304 Status code, otherwise it will forward the request to nginx listening at port 8080 and it would return the response with HTTP status 200.
Reference:

  • Nginx - https://en.wikipedia.org/wiki/Nginx
  • Varnish - https://www.varnish-cache.org/about

Tuesday, September 8, 2015

Varnish on Windows (using Cygwin)

This post details how to run Varnish on Windows (using Cygwin) .

Install Varnish and Cygwin on Windows

To install Varnish and Cygwin on Windows, refer to download and installation instructions at:
https://www.varnish-cache.org/trac/wiki/VarnishOnCygwinWindows#InstallFullCygwinenvironmentwithvarnishpackage

Configure default.vcl file

  • Take a copy of default.vcl file located at {cygwin_install}/etc/varnish
  • Now open default.vcl and configure backend default
I am giving port 3000 because it is the default port for node.js

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


Start up Varnish

On cygwin window, type following command to startup varnish
$ /usr/sbin/varnishd -d -f ./etc/varnish/default.vcl -a 127.0.0.1:6081

This will start up Varnish in debug mode, so that you can monitor it.

Once started, list the processes
$ ps -ef | grep varnish

you should see varnish process started.

Issues with Varnish on Cygwin and solution:

Panic message: Assert error in sock_test(), /usr/src/varnish-3.0.5-1/src/varnish-3.0.5/bin/varnishd/cache_acceptor.c line 166:

  Condition(l == sizeof tcp_nodelay) not true.

Solution:
You will need to download the varnish src (varnish-3.0.5-1-src.tar.xz) and remove the assert statement at varnish-3.0.5/bin/varnishd/cache_acceptor.c line 166

Then recompile and install:
  • cygport varnish-{version}.cygport prep
  • cygport varnish-{version}.cygport compile
  • cygport varnish-{version}.cygport install

More details for compile at - http://sourceforge.net/projects/cygvarnish/files/cygport-packages/64%20bits%20%28x86_64%29/