Configuring NGINX for Jekyll on Ubuntu

Published on August 10, 2011

After a few years of shared hosting, a few days ago i moved this site on a Linode VPS and so far i am really happy with it, the performances are impressive, especially for what concern disk i/o, way better then what you get with other VPS providers.

With the new hosting i decided it was time to finally drop Wordpress and the boatload of plugins i was using and try an alternative solution that was more appropriate for a site with a small number of pages and no dynamic content like mine.

The old site was already using a caching plugin for WordPress, W3 Total Cache, to produce static pages (regenerated periodically), on the new site i am giving a try to Jekyll to remove some moving parts and simply serve a static site built offline with a generator.

So far i have no complains, i would have preferred if a basic layout with archive/categories/tags/etc… was bundled with Jekyll but checking the numerous examples available on github i’ve found my way through anyway, and built an initial skeletal layout in a few days than then evolved in what you are seeing now.

As my HTTP server of choice, i have opted for NGINX. Various independent benchmarks performed with different environments and configurations i have read lately seems to confirm that, with the right configuration, NGINX is one of the best http server for static content serving.

If interested you can read some of them here, here and here.

This kind of tests should be taken with a grain of salt, after all the only meaningful benchmark for you is the one you’ll perform on your system with your configuration, here is an interesting discussion on the rules of benchmarking.

This is the configuration i am using at the moment, it’s optimized for small files and it enables the file cache to reduce disk access and the number of i/o syscalls. I have performed a few tests on my own and this seems to perform quite well on the first two VPS tier of Linode.


user www-data;
worker_processes  2;

events {
	worker_connections  1024;
}

http {

	include mime.types;
	default_type  application/octet-stream;

	log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
		'$status $body_bytes_sent "$http_referer" '
		'"$http_user_agent" "$http_x_forwarded_for"';

	open_file_cache max=1000 inactive=20s;
	open_file_cache_valid 30s;
	open_file_cache_min_uses 2;

	sendfile off;
	tcp_nodelay on;

	keepalive_timeout  65;

	server {
		listen 80;
		server_name <yourdomain>;
		access_log /var/www/logs/<yourdomain>.access.log  main;
		error_log /var/www/logs/<yourdomain>.error.log;
		root /var/www/sites/<yourdomain>;
		index index.html index.htm;
	}
}

On the NGINX wiki you can find a nice init.d script to launch the server but to manage its lifecycle i suggest to use upstart (installed by default on ubuntu) instead, it will manage your NGINX instance restarting it if it dies unexpectedly.

Create an upstart configuration file at /etc/init/nginx.conf:


description "nginx http daemon"

start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]

env DAEMON=/usr/local/nginx/sbin/nginx

expect fork
respawn

pre-start script
	$DAEMON -t
	if [ $? -ne 0 ]
		then exit $?
	fi
end script

exec $DAEMON -c /etc/nginx/nginx.conf

Verify that upstart has located your new configuration file, if the output of the following command is empty you should check /var/log/daemon.log for errors in the config file:


initctl list | grep nginx

Now you can start NGINX with:


initctl start nginx

Your NGINX instance will now be respawned if it dies unexpectedly and this can easily be verified with:


killall nginx
ps -Al | grep nginx

Did you like this article? Follow me on Twitter!

Receive a notification every time I publish a new article, no spam.

This is the blog of Umberto Raimondi, you can reach me at me@this domain.

I'm also on Twitter and GitHub.

Subscribe via RSS or email.