Nginx 101 — Part 1

Sachith Muhandiram
3 min readJun 30, 2020
https://tinyurl.com/y8fgdzbf

Today I am gonna talk about something I started to learn recently. I have used nginx many times, but didnt have much deeper understanding. So I decided to dig deeper. Luckily I found Hussein Nasser’s Nginx series. I highly recommend you to follow it. Here in this post, I will try to give you a briefer description about nginx and simplest configuration to run and serve our sites. So lets get started.

What is Nginx

Nginx basically has two main features.

  • Web Server
  • Proxy (Layer 7 and Layer 4)

But here we will focus on the most important part of nginx, Proxy. I wont describe what is a proxy and how it works deeply. Simply a proxy is a thing that hides backend to frontend users. This frontend — backend connecting can be done based on many smart decisions. We will cover some in this post.

Install Nginx

Simply follow the given instructions given here and verify your installation working successfully. Check where is your nginx.conf is located. For Ubuntu 20.04, I had it under /etc/nginx/nginx.conf .

You can use official instructions here. Nginx supports 4 signals commands. nginx -s signal

  • reload — reloads configuration file.
  • stop — stops running nginx (fast shutdown)
  • quit — stops nginx (gracefully)
  • reopen — opens log files ( we would need this later)

Creating the simplest nginx web server

First thing first, backup your original nginx.conf file and then only delete the content in original nginx.conf file to start from scratch.

http {server {
listen 8000;
}
} # http blockevents { } # events block

This is the simplest nginx web server running localhost:8000 , before restart nginx lets find out what are these different blocks and their purpose in this configuration file.

Here from http and events blocks considered as main components. Any config file MUST have them. This example, we use nginx as a layer-7 loadbalancer. Thats what’s why we need http block. If we want to use nginx as layer-4 LB, it should be stream .

Inside http block, we have our server { } , this part we define our actual nginx server configurations. In this example port we are gonna listen. Here nginx will listen to localhost:8000

Next its events { } , this configuration is mainly used to define event-based connections. Simply how worker process should handle connections. This is done automatically. For linux systems its epol etc. You can read more from here.

Testing

After you enter this configurations to /etc/nginx/nginx.conf file, save and exit. Open a terminal and run nginx -s reload , as described above, this will load our new configuration file to nginx instance. If reload steps gets an error, it will give you what went wrong similar to below one.

nginx: [emerg] directive “upstream” has no opening “{“ in /etc/nginx/nginx.conf:4

If you have no errors, you can visit localhost:8000 and you will see default nginx web page.

So in next time, lets create a layer-7 loadbalancer using nginx.

--

--