Configuring Nginx web server

Published on July 01, 2009

Configuring Nginx

Installing Nginx

This tutorial is specific to Ubuntu Linux, but can be adapted for other versions of *nix and Linux distros. Here, we assume that

  • Nginx is installed in /etc/nginx/
  • The configuration file nginx.conf is located at /etc/nginx/
  • Nginx can be started by running this command:

    sudo /etc/init.d/nginx start

Getting started

A typical nginx.conf file looks like this (at least the one on my development machine)

user www-data;
worker_processes  2;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    # turn directory listing off 
    autoindex off;

    # turn on gzip compression
    gzip  on;

    # load all domains
    include /etc/nginx/sites-enabled/*;
}

What is www-data?

user www-data means Nginx is going to run as user www-data. This is useful if you're making the transition from Apache, where the user is www-data. You won't have to change ownership of any existing files or directories.

You can have a maximum of 2048 clients connected. This number is got by multiplying worker_processes with worker_connections. autoindex is turned off for safety, to prevent visitors from snooping.

gzip is turned on so that Nginx serves compressed web content - save on bandwidth.

The other lines are self-explanatory.

The domain configuration files are located in /etc/nginx/sites-enabled/

Configuring the domain

Suppose we want to use Nginx to host the domain imacster.com this is a basic configuration.

  • the files are hosted at /home/username/public_html/imacstercom
  • the logs will be stored in /etc/nginx/logs/imacstercom
  • 40x.html, 50x.html and maintenance.html are static files for error display

Contents of /etc/nginx/sites-enabled/default

server {
    listen       80;
    server_name  imacster.com;

    # log files
    access_log /etc/nginx/logs/imacstercom/access.log;
    error_log /etc/nginx/logs/imacstercom/error.log warn;

    # directory
    root   /home/username/public_html/imacstercom;
    index  index.html index.php;

    location / {
    }

    error_page   401 403 404      /40x.html;
    error_page   500 502 504      /50x.html;
    error_page   503              @503;
    location @503 {
        rewrite ^(.*)$ /maintenance.html break;
    }
}

That's the basic configuration for Nginx. To include support for PHP scripts, there are 2 ways.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on July 01, 2009