Getting Started with NGINX

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption (SOURCE)


Installation of NGINX in its self is super easy. There is an up to date package on the Ubuntu Repository.

sudo apt-get install nginx

Once we’ve installed NGINX, we want to start it, and enable it so we can ensure it starts upon reboot.

sudo systemctl start nginx && sudo systemctl enable nginx

Now that we’ve started NGINX, we’ll be working primarily in two directories. The first is /etc/nginx/. This is our working NGINX directory, and is where we’ll make any changes to NGINX or its files. The second will be /var/www. This is our working directory for any site files.
Because we’ll be working in /var/www, we want to ensure that NGINX has the correct permissions to maintain the files located there.

sudo chown -R www-data:www-data /var/www

Creating Server Blocks

Server blocks are a big part about how NGINX manages multiple websites on a single server. These server blocks will contain settings and information that are specific to each site you create, as well as tell NGINX where those files are located. Server blocks are the equivalent to Virtual Hosts on Apache.
When creating a server block, we should be working in /etc/nginx/sites-available. The reason we work in this directory is because it allows us to work and manage all of our files, without having them be accessible. This is useful for when you take access to a site down, or are working on edits to the server block. Later on, we’ll link these to /etc/nginx/sites-enabled, which is where accessible sites live.
By default, the /etc/nginx/sites-available directory includes a default file. This default file includes a ton of information and comments out a lot. For new guys, it can be a bit over whelming. For that reason, I prefer to delete it and work with my own files, so we are going to do that now.

sudo rm /etc/nginx/sites-available/default && sudo rm /etc/nginx/sites-enabled/default

Now that we have a blank working directory, lets go ahead and create our own default file. The default file we’ll create is the same as NGINX’s default file, with anything thats commented out removed. This makes it easier for new users to sift through it. sudo nano default. Then we’ll add the following:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                try_files $uri $uri/ =404;

One thing to notice right off the bat in our listen directives, is that it indicates the default_server. Now, one thing to keep in mind is that we can only have ONE server block with this default_server option. This option indicates which server block will be served should the incoming request not match any of the server_name‘s available. Ideally, this doesn’t occur since most users will be accessing your website via a domain. As you add more websites, you’ll want to determine which site you want to flag as the default_server. This can be changed at anytime with little effort so it doesnt have to be something that you put a lot of effort into deciding now.

You can check to see if the default_server option is enabled on more than one server block by using the following line:
grep -R default_server /etc/nginx/sites-enabled/

The next part of the server block that we need to look at is the root. This is the root directory of the site we’re working in. By default thats /var/www/html. We can adjust this to any directory on the machine, just keep in mind that you want to adjust the permissions on that directory so that NGINX can properly access it. For this example, we’ll continue to use /var/www/html.
Underneath rootis the basically our order of operations for accessing the content. In the default configuration we see index index.html index.htm index.nginx-debian.html;. This means that when we access the site, it will look for an index file. If it does not exist, it will look for index.html and so on. So for example, if you wanted the server to look for home.html first for example, you could edit your file to read home.html index index.html index.htm index.nginx-debian.html;.
Additionally, we want to look at server_name. The server_name is what NGINX is listening for. So for example, since my site is, I’d replace server_name _; with server_name;. Basically, this means that when NGINX sees, it knows we want to access the settings, and files outlines in this server block.
The last section location / { shows us that in the root directory, if it cant find the file in the url, it will return a 404 page, rather than returning a browser error.

Now that we’ve created our default server block, we need to create a test page to ensure that NGINX is up and running as expected. So we’ll head on over to our document root that we specified in the server block, so in this case /var/www/html. We’ll want to create an index.html file with the following contents:

        <title>Welcome to!</title>
        <h1>Success!  The server block is working!</h1>

Once we’ve saved the index.html file, we want to link it to /etc/nginx/sites-enabled and then test our configuration.
To link our server block from /etc/nginx/sites-available to /etc/nginx-sites-enabled, we are going to create a symbolic link. This way, when we change either file, it updates the other location. To do this, we’ll go ahead and do sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/.
To test our configuration we’ll use sudo nginx -t and if everything checks out, we’ll receive the following:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If everything checks out, we’ll do sudo nginx -s reload to reload NGINX and then you should be able to access your site via http://http://<ipaddress>.