Where Insanity Thrives… Still

Well so as not to leave this to long I thought I would start with how I went about setting up Nginx (the software that is serving you this page)

My configuration is designed to allow me to easily add extra domains on as virtual hosts without having to change the server config files. while also giving me the flexibility to add new services on other ports (like web mail) with a minimum of fuss.

I won’t go in to the specifics of installing Nginx as im sure you will find that on a million other blog posts or your chosen distribution’s documentation.

after installing and checking that Nginx will serve web pages the first step is to make a few changes to the way that Nginx handles files, particularly if you want to serve PHP files with it.

so lets take a look at my nginx.conf file:

user www-data;
worker_processes  1;
error_log  /var/log/nginx/error.log;
pid        /var/run/;
events {
    worker_connections  1024;
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    access_log  /var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;
    tcp_nodelay        on;
    gzip  off;
    server_name_in_redirect off;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen 80;
        server_name _;
        root /var/www/$host/html/;

        allow all;
        location / {
            index index.php index.html index.htm;
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|htm)$ {
            access_log off;
            expires 30d;
        location ~ .php$ {
            fastcgi_index index.php;
            include fastcgi_params;
       location ~ /\.ht {
           deny  all;

    include /etc/nginx/ports/*.conf;

As you can see I am only starting 1 worker process, meaning only one page will be served at a time, this is fine for a small traffic site, but if it starts to get more traffic then I would need to increase this number to improve the response time of the server. we then set the server wide defaults, with the error log file and how many connections the worker will take before it refreshes.

Then we go in to the magic section of the file that allows all our virtual hosts to work.

This server directive listens on port 80 and will accept connections with any server name (i.e. domain name) it will then set the root directory to a directory specified by the host name that is sent with the request so for this site it is set to /var/www/ we then give the index files, in this case html files and php files, and lastly the location directives, working from last to first (the way the priority for NginX works, .htaccess files are not allowed to be viewed over this connections, php files are parsed by extension (I’m yet to find a better way to do this) to the fastCGI server listening on port 9000 and all its parameters are set in the fastcfi_params file (this file will be explained in a later post) then all static files (images and html files) are sent directly and not logged in the access log.

Then once that default port 80 server is define we include all files in the ports directory that i created where i have a set of config files that define the different applications that run on other ports (web mail etc)

This config allows me to add a new domain by simply pointing its DNS records to the server and creating a directory in /var/www/ to hold its web files, I don’t have to restart NginX, change config files etc. making it quite useful in a number of situations including if you wanted to host a number of other domains that arnt yours (say your a hosting company) and a huge advantage over apache it only uses less than 2Mb of memory per worker process, and this does NOT grow depending on how much traffic you are getting, so start your server, check your memory usage, and you know that it will sit at that level and not drop into paging hell should you, or another site on the server be luck enough to experience the /. effect.

Tags: , , , ,

Powered by Wordpress
Theme © 2005 - 2009
BlueMod is a modification of the blueblog_DE Theme by Oliver Wunder