Nginx tutorial for beginners

NGINX Tutorial for Beginners (NEW) – Learn for FREE !

If you are preparing for any of the Linux Certification training, or Cloud computing certification training, you will be seeing this term ‘Nginx’ in your exam objectives. What exactly is Nginx? and What it does – We are going to explore more in this topic now.

This Nginx tutorial for beginners, helps you to understand the features of this web-server and how to use them in Ubuntu Operating system. This can be used in any popular operating system and servers. Let’s start !

What is Nginx?

Nginx is one of the most widely used opensource webserver. A Web server is simply a program that uses HTTP to serve files to users as a response. Nginx is one of the most popular web-server available in the industry. On top of this basic use-case, Nginx has other different use cases as well such as –

  • Adding capabilities of handling multiple domain routes.
  • Acting as a reverse proxy.
  • Carrying out load balancing.

In this post, we will understand the basics of Nginx, its installation and explore different use cases.

Nginx web server as Load balancer
Image credit: www.medium.com

Installing Nginx

This tutorial is using Nginx on Ubuntu. For any other operating system installation, refer to the Nginx docs.

The below commands update the Ubuntu OS and then install Nginx.

sudo apt update -y
sudo apt install nginx -y

For different operations on Nginx such as start, restart, reload, etc – refer to the below commands.

sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx

That’s all the commands that you need to know to get started with Nginx.

Nginx processes

Nginx works by spawning multiple processes of two kinds i.e. Master process & Worker process.

The master process is responsible for reading & taking actions according to the configuration file. It also creates & manages the worker processes.

The worker process is responsible for handling the server requests and replying with responses.

Nginx configurations

Nginx configuration file can be viewed at /etc/nginx/nginx.conf in Ubuntu.

The most important part of using Nginx is about understanding the different configurations on the Nginx configuration file. The most important ones have been explained below.

  • The user directive specifies the user by which the worker processes would be spun up.
    user www-data;
  • The worker_processes directive specifies the no. of worker process that the master process needs to create & manage. If set to “auto”, the master process would create/delete worker processes according to load.
    worker_processes auto;
  • The pid directive specified the location of a file that has the Nginx process id.
    pid /run/nginx.pid;
  • The access_log directive specifies the location of the nginx logs.

    http {
        access_log /var/log/nginx/access.log;
    }
  • The error_log directive specifies the location of the nginx error logs.
    http {
        error_log /var/log/nginx/error.log;
    }
  • The multiple include directive specifies additional files that need to be loaded as configurations.
    include /etc/nginx/modules-enabled/*.conf;
    
    http {
       include /etc/nginx/mime.types;
       include /etc/nginx/conf.d/*.conf;
       include /etc/nginx/sites-enabled/*;
    }

Trying out Nginx

Let’s try out Nginx now!

By default, Nginx listens on port 80 and serves a basic HTML page. Try it out –

curl http://localhost:80

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Let’s try to serve our own HTML page using Nginx now. This will help in developing an understanding of Nginx server configurations.

Serving a custom webpage

Let’s first create our webpage at /usr/share/nginx/html/index.html –

<!DOCTYPE html>
<html>
<head>
<title>Custom webpage using Nginx</title>
</head>
<body>
<p><em>Custom webpage using Nginx</em></p>
</body>
</html>

Now, let’s add the server configurations at /etc/nginx/conf.d/default.conf –

server {
  listen 80;
  server_name localhost;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm; 
  }
}

Here, the “location /” means that whenever a request comes to the “/” path on address ‘localhost’ on port ’80’, Nginx serves index.html from /usr/share/nginx/html.

It’s a good practice to verify the Nginx configuration before applying the change. In order to do that –

sudo nginx -t

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

After this, it is safe to reload Nginx with the new configurations –

sudo systemctl reload nginx

Now, try out –

curl http://localhost:80

<!DOCTYPE html>
<html>
<head>
<title>Custom webpage using Nginx</title>
</head>
<body>
<p><em>Custom webpage using Nginx</em></p>
</body>
</html>

It’s that easy!

Now, let’s try a popular Nginx use case in which the Nginx server is used as a reverse proxy for backend servers.

Nginx as a reverse proxy

When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client.

In this guide, we will make use of two servers – backend 1 as the proxy server and backend 2 as the server sending the response.

Let’s configure backend 2 first.

Its configuration would be similar to setting up a simple static webpage, like above.

Create an index.html file at /usr/share/nginx/html/index.html

<!DOCTYPE html>
<html>
<head>
<title>backend 2</title>
</head>
<body>
<p><em>This is backend 2</em></p>
</body>
</html>

Modify the Nginx configuration file at /etc/nginx/conf.d/default.conf

server {
  listen 80;
  server_name 3.93.215.182;

  location / {
   root /usr/share/nginx/html;
   index index.html index.htm; 
  }
}

Here the IP address is that of the backend 2 servers. We cannot use ‘localhost’ here, since the backend 1 server is going to use the IP address to proxy requests to backend 2.

Verify the changes and reload the configuration

sudo nginx -t
sudo systemctl reload nginx

Try it out

curl http://3.93.215.182:80/

Now, let’s configure the backend 1 server as the proxy. SSH into backend 1.

Modify the nginx configuration at /etc/nginx/conf.d/default.conf

server {
  listen 80;
  server_name 3.93.215.182;

  location / {
   proxy_pass http://3.93.215.182:80/
  }
}

To pass a request to an HTTP proxied server, the proxy_pass directive is specified inside a location.

Verify the changes and reload the configuration

sudo nginx -t
sudo systemctl reload nginx

Try it out

curl http://localhost:80/

You should see the response coming from backend 2.

That’s it. Backend 1 is serving as a proxy for requests to backend 2.

Summary

Hope you have enjoyed the Nginx tutorial for beginners. In this post, we have covered the basic use cases of Nginx. Nginx is used for a variety of other use cases as well. By trying these sample codes, you can master Nginx webserver. Enjoy trying out Nginx which is primarily designed for web serving, caching, load balancing, reverse proxying etc.

About Shishir Khandelwal

Shishir has the passion and zeal to master his field of cloud & containers. He is a strong advocate of finding smart solutions to complex problems by leveraging the power of cloud & container technology such as Kubernetes and a strong believer in learning by doing because of which he does a lot of POCs and personal projects. He is a certified expert in AWS & Kubernetes.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top