So what actually is Nginx? (Linux Sys-Admin for web devs) cover image

So what actually is Nginx? (Linux Sys-Admin for web devs)

Angus Allman • February 7, 2020

nginx deployment servers what is nginx

We've all been there

Getting to the point of having a site looking beautiful and polished with only one problem... no one can actually see it yet. This is where the whole painful deployment process kicks off. A search for "How to deploy a your stack site" will return a slew of options but some things seem to crop up time and time again no matter what tech you're using. Nginx

What is it?

The official word (from the Nginx site) is:

NGINX is open-source software for web serving, reverse proxying, caching, load balancing, media streaming, and more.

Put simply, it's the thing that handles communication between the web server and your site. Whether that be reading the domain that's been requested by the user and handling it by pointing the request to the correct files, or sending it elsewhere (reverse proxy).

When you access a website, say ilovenginx.com, it always sends a request to a server somewhere in the world (for more info on how this works read about DNS). Now that the request's reached the server it needs to know what to do with it and that's where Nginx comes in. Using it you can create config files that contain instructions for what to do when a request to ilovenginx.com arrives at the server.

Hit the ground running

Nginx creates a load of files in the /etc/nginx directory as soon as it's installed. This is where you want to create your config files because without them Nginx is useless and won't do a thing.

Inside /etc/nginx and you'll see a myriad of files and folders but the ones we want are sites-enabled, and sites-available.

sites-available is where we create and edit our Nginx config files and sites-enabled contains symbolic links (symlinks) to our config files in sites-available. The point of this is that you can have as many config files as you want available to use but only actually have the important ones which your site(s) use enabled.

The config files

If you head into the directory /etc/nginx/sites-available there will be a file named 000-default.conf. If you want you can open that up and take a look at the base config file template.

I'm fairly sure that these config files look bewildering to even the most experienced developers and I'll be creating a line-by-line breakdown of one of these files in another blog post (coming soon) but let's not get bogged down in the details here. Fundamentally, these files are a list of instructions that tell Nginx what to do with requests that arrive depending on which domain they're for and what port they arrive at (usually port 80 for HTTP requests, or port 443 for HTTPS).

A brief rundown

As with all systems like this, there is a great deal which I could discuss here and an endless list of varying commands which do plenty of different, unique things, however below, I’ve listed a few of the most important lines from a config file with a brief explanation of what they do.

server {} - Creates a new Nginx server to handle requests (all Nginx commands should be within a server block)

listen - Defines which port the this server should handle requests from.

root - Points to the directory which your site is stored in. The path will usually contain /var/www if you use Nginx's defaults.

index - Tells Nginx a list of files in the root directory to use to run the site. You can use space-separated file names in the order which they should be tried

server_name - Defines the domain name that you want this server block to handle requests for. You can list multiple domains here e.g:

server_name example.com www.example.com;

Conclusion

That just about does it for the basics of Nginx! While it can seem complicated and like something that only advanced devs know there’s no need for it to stay that way. What it does under the hood is all kinds of magic but at its core it’s an elegant way to tell your server how to handle requests, get your site live and show it to the world. As I mentioned there will be another post coming soon which will be a deep dive into a real Nginx config file and how it does its wizardry line-by-line. If there is anything you feel could be added to this post or you have any questions then leave them in the comments below!