Introduction to Nginx
Tagged with nginx
Hello and welcome to this introductory tutorial on Nginx. The goal of this tutorial is to give you a hands on walkthrough of Nginx including what it is, how it works, and what its key features are.
So before we go too far, it might be wise to ask, what exactly is Nginx? Well, in the beginning, Nginx was designed as a simple HTTP server intended to optimize serving static files as quickly and reliably as possible. Over time, more features began to be added to accommodate the growing needs of users, turning it into what we have now.
In essence, Nginx is a free open source HTTP server. Some would consider it competitive to Apache, however I would not consider this a truly valid comparison as each have their unique architectures and strong suits. One thing Apache is known to do well is acting as a full application server with which you could run PHP, Ruby, Python, and other backend languages on to handle server functionalities. Nginx on the other hand is not built with this in mind. Instead it excels in serving static websites, single page applications, acting as a reverse proxy, acting as a load balancer, and/or providing a cache for to enhance performance. When evaluating your project requirements, if these are the key things you are looking for, then Nginx is the prime choice for you. If you need an application server, you may be best suited looking else where. However, you can always use Nginx to proxy for your other application servers as needed.
So what are some key characteristics of Nginx that make it unique? First, we should discuss Nginx's asynchronous model for handling tasks. A simplified way of understanding it could be to think of a line in a grocery store. Imagine a checkout line with customers buying their groceries. Everything is going smoothly until we see one customer with a cart loaded to the max with grocery items. When they begin to check out, they hold all of the other customers up, the line grows and we have a problem brewing. The Apache way of handling it would be to open another grocery line (thread) so more customers, aka tasks, can be cleared. The Nginx approach on the other hand would be rather different. When the customer with tons of groceries approaches the checkout line, the cashier would then have the customer begin unloading their groceries while going ahead and checking out the customers with drastically smaller carts. The end result being, we achieve getting our customers through without having to use extra resources. In other words, Nginx can achieve high performance without using the same amount of resources. However, it is key to understand that this isn't single threaded like Node.js. Instead Nginx operates with a master process and a series of worker processes that handle various loads that we demand of it.
Next, it would be wise to point out Nginx's modularity. When building Nginx from source, you can customize it using plugins that can handle a whole assortment of things from SSL to authentication and even optimization for particular languages. There is also a rich 3rd party community dedicated to delivering plugins to enhance Nginx with more functionalities that you may or may not need in your own productio scenarios.
Prebuild binaries on the other hand will already contain a selection of the most important and widely used modules that are needed in day to day use. We will be specifically using a prebuilt binary that we can install using a standard package manager. Speaking of prebuilt binaries, let's set up a droplet on Digital Ocean and install Nginx.
ssh freebsd@your_nginx_ip_address sudo passwd sudo pkg search nano sudo pkg install nano sudo chown freebsd /etc/rc.conf
Good, now let's open the
/etc/rc.conf file in our text editor or choice. You can use
Nano or Vi, but to make things easier to read, I will use Sublime Text with the SFTP
# Firewall Configuration firewall_enable="YES" firewall_quiet="YES" firewall_type="workstation" firewall_myservices="22 80" firewall_allowservices="any" firewall_logdeny="YES" nginx_enable="YES"
sudo service ipfw start sudo nano /etc/sysctl.conf
sudo nano /etc/rc.conf
# ntpd config ntpd_enable="YES" ntpd_start_on_sync="YES"
sudo service ntpd start sudo tzsetup clear
sudo pkg install nginx sudo nano /etc/rc.conf
# nginx nginx_enable="YES"
sudo service nginx start
Now, let's open the main Nginx config file.
sudo nano /usr/local/etc/nginx/nginx.conf
Great, now we can take a look around and get an idea of how Nginx scripting works. First, let's take a moment to grasp Nginx directives. It's key to understand that in Nginx we have simple directives then we have block directives.
A simple directive is just a rule that you have written, much like a statement in a line of code. You have the name of the function you are essentially calling, followed by one or more parameters that are separated by spaces, and the line ends with a semicolon.
So what about block directives? First, take a look at the
http block with the curly
braces. That is a block directive. It functions much like you would expect from other
languages like C or C++. A block directive allows you to define numerous simple directives
that apply to a specific context. In this case,
http is used as a core directive for
handling all http networking issues. If you look inside of
http however, you'll see a
server blocks. Each of these blocks are defining actual web servers. So
if you were wanting to use Nginx to host a series of websites, this would be where you
defined each website.
Now of course, with Nginx giving all these example server blocks, it gives you a chance to see how all of this works. You can see a normal webserver here, a virtual host here, then you have an HTTPS example located here.
For security reasons, it's also worth taking specific directory called directory_listing. We won't insert it into the configuration, however it's good to be aware that this is a directive that, when activated to on, allow people to comb through your server file system. It is highly advised to never turn this on but it is also important to be aware of it in case someone else does turn it on for your production server.
So we've managed to get a brief introduction to Nginx, what it is, and how it works. We also managed to install Nginx on a FreeBSD instance then get it configured and running. Tomorrow, we will turn our attention to hosting and scaling static websites using Nginx.
Alex is an independent developer who is obsessed with both performance and information security. When not writing code, Alex is either playing guitar or working in his garden.