Roland's homepage

My random knot in the Web

Nginx webserver setup

After having written my website by hand for years, I've switched to the pelican website generator. To test this generated site I needed a webserver.

Over the years I've used apache, lighttpd and thttpd. But none of those really worked like I wanted to. So I thought I would try nginx.

Since it is in the FreeBSD ports, installing it was easy;

# cd /usr/ports/www/nginx
# make config
# make install clean

There are a lot of optional enhancement modules available for nginx, but I only activated a couple of them;

  • http
  • http_cache
  • http_rewrite
  • file aio
  • http_fancyindex

With the documentation from the wiki, settting it up was not that hard. Below is the configuration file used for serving my simple static website;

# -*- text -*-
# file: /usr/local/etc/nginx/nginx.conf
# host: slackbox.erewhon.net
# $Date: 2012-12-31 12:04:08 +0100 $

user  www www;
worker_processes  1;

error_log  /var/log/nginx/error.log  info;

events {
        use kqueue;
        worker_connections  512;
}

http {
        include mime.types;
        default_type application/octet-stream;
        access_log /var/log/nginx/localhost.access.log;
        sendfile on;
        keepalive_timeout 65;
        server { # Standard static website
                server_name www.erewhon.net;
                                    listen localhost:80
                root /home/rsmith/WWW/output;
                index index.html;
                error_page  404 /404.html;
                location = /404.html {
                        root /usr/local/www;
                }
                # redirect server error pages to the static page /50x.html
                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                        root /usr/local/www/nginx-dist;
                }
        }
        server { # For serving media files on the network
                server_name media.erewhon.net;
                                    listen localhost:80
                root /home/media;
                fancyindex on;
                fancyindex_exact_size off;
        }
}

In my /etc/hosts file, the name www.erewhon.net resolves to the localhost. As a security precaution, I've restricted nginx to only listen on the localhost address.