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 (updated 2016-12-11);

# file: nginx.conf
# vim:fileencoding=utf-8:ft=conf
#
# Author: R.F. Smith <rsmith@xs4all.nl>
# Created: 2016-09-23 22:19:55 +0200
# Last modified: 2016-12-11 18:34:51 +0100

user  www www;
worker_processes  1;

events {
    use kqueue;
    multi_accept on;
    worker_connections  2048;
}

http {
    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    access_log off;

    keepalive_timeout 10;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 10;

    limit_conn_zone $binary_remote_addr zone=addr:5m;
    limit_conn addr 100;

    include mime.types;
    default_type text/html;
    charset UTF-8;

    gzip on;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_buffers 16 8k;
    gzip_comp_level 4;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    server { # Standard static website
        server_name www.erewhon.home;
        listen localhost:80;
        root /home/www;
        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.home;
        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.


←  Planning Switching to the vim editor  →