Share this page:

Nginx Basics

Nginx is the second most popular open source web server behind Apache. Nginx is open source, but some add-ons are available as part of a paid subscription. According to W3Techs.com, it is used in approximately 23% of all websites.

Nginx uses an asynchronous event processing model instead of a thread based model. Apache needs one thread per connection, whereas Nginx uses one thread per CPU, and each Nginx thread can handle many connections.  Nginx tends to use less memory than Apache, and performs very well under a heavy load.

Before installing Nginx, make sure that your server is up to date.

sudo apt-get update
sudo apt-get upgrade

Now install and start Nginx:

sudo apt-get install nginx
sudo service nginx start

Visit your server's IP address in your browser, and you should see a sample page like this:

Nginx sample page

Configuration files

The configuration files for Nginx are stored in /etc/nginx.  There are some global settings in /etc/nginx/nginx.conf that control the server as a whole, and /etc/nginx/sites-available contains configuration files for individual virtual servers.  

Virtual servers are a lot like virtual hosts in Apache; they are a groups of configuration options that describe how the server should handle traffic for different sites.

Settings are grouped into contexts.  Some contexts can contain other contexts, creating a hierarchy. The http context contains settings that control the HTTP protocol. Virtual server settings are stored in server contexts, which are contained in the http context.  Locations are contexts that store settings for URLs, and they are contained within a server context.

http {
    server {
        # Server configuration

        location /some_url {

        }
    }
    server {
        location /some_other_url {

        }
    }
}

Server contexts are usually stored in a seperate file than the http context, and the include directive is used to import code for virtual servers.  This line is used in the http context to include site-specific configuration files:

include /etc/nginx/sites-enabled/*

nginx.conf

Nginx.conf contains server-wide options. The first options set Nginx's username and the number of worker processes:

user www-data;
worker_processes 4;

The number of worker processes should be set to the number of CPU cores in the server. There's no benefit to creating additional processes.

The events context contains a setting named worker_connections which determines how many simultaneous connections Nginx will attempt to handle.  If you increase this number, make sure that you also increase the maximum number of files that the OS can open with ulimit.

events {
    worker_connections 768;
    # multi_accept on;
}

The http context contains settings for handling TCP connections, logging and content compression with gzip.

Extra configuration files can be stored in /etc/nginx/conf.d/. This line of code ensures that as long as they have a .conf extension, they will be included in nginx.conf.

include /etc/nginx/conf.d/*.conf

Comments

Nginx Virtual Servers

Virtual server files are stored in /etc/nginx/sites-available.  When a site is enabled, a file system link is created from the virtual server file in /etc/nginx/sites-available to /etc/nginx/sites-enabled. Nginx looks for virtual server files in /etc/nginx/sites-enabled and uses links to access files in /etc/nginx/sites-available.

Enabling and disabling sites with Nginx

It's customary to name virtual server files after the site that they represent.  If you have a site named www.example.com, you would put the settings in /etc/nginx/sites-available/www.example.com.conf. To enable the site, create a link to /etc/nginx/sites-enabled/:

ln -s /etc/nginx/sites-available/www.example.com.conf /etc/nginx/sites-enabled/

To disable a site, simply delete the link from /etc/nginx/sites-enabled/:

rm /etc/nginx/sites-enabled/www.example.com.conf

Nginx needs to be restarted after a site has been enabled or disabled.

Server contexts

Server contexts contain the settings for a virtual server. The following directives are used in server contexts:

  • listen - tell Nginx which port to listen on. The default is port 80. 
  • root - path to the web root directory. The default is /usr/share/nginx/www/
  • index - files that should be served if a directory url is a directory
  • server_name - Nginx will use this virtual host to handle requests with a host field that matches server_name

Locations

Locations are defined in server contexts, and they tell Nginx how to handle requests for specific URIs. 

Locations often use the try_files directive to tell Nginx to try a succession of different methods for serving a page.

location / {
    try_files $uri $uri/ /index.html;
}

This makes Nginx look for a file matching the value stored in $uri.  If the requested URL is http://example.com/foo.html, the value of $uri is /foo.html, so Nginx will look for a file called foo.html and serve it if it's available.  If not, Nginx will check to see if there is a directory named /foo/.  If no such directory exists, Nginx will default to serving a page called index.html. 

In the following example, Nginx will default to redirecting the request to index.php, and pass the value of $uri as variable 'q':

location / {
    try_files $uri $uri/ /index.php?q=$uri;
}

A location can be defined for a specific page:

location = /foo.html {
    ...
}

Locations can be defined for an entire directory:

location /bar/ {
    ...
}

The settings in this location will apply to any URI that starts with '/bar'

Access control

Access to locations can be allowed or denied according to the client's IP address. In this example, access is denied for IP address 192.168.1.4.  This location can be accessed from all other IP addresses on subnet 192.168.1 (the '/24' means that only the first 24 bits of the IP address will be considered). Access from all other IP addresses is diabled.

location = /somefile.html {
    deny 192.168.1.4;
    allow 192.168.1.0/24;
    deny all;
}

Variables

Nginx supports the use of variables in its configuration files.  Variables start with a '$'. This page contains a list of all the variables available in Nginx.

For example, if you're hosting several sites on a server, you can use the $host variable to append the host to the end of the root directive.

root /usr/share/nginx/www/$host

This is useful if /usr/share/nginx/www/ contains a directory for each site named after the site's domain name.


Comments