NGINX For Devops Engineers

Ghazanfar Ali
5 min readOct 2, 2023

--

Introduction:

Today we will learn what is nginx and deploy the docker based app.

What is Nginx?

Nginx is a webserver which serves your web files, nginx architecture is very strong as compare to other web servers, nginx master process control many child processes, so whenever any user came with the request it treats as a separate process.

Some more Features of NGINX:

1- Reverse Proxy:

Nginx reverse proxy acts like a middleman, receiving requests from clients and forwarding them to the appropriate web servers, making it easier to manage multiple websites or applications on a single server while enhancing security and performance.

2- Load Balancing:

Nginx load balancing distributes incoming web traffic across multiple servers, ensuring even work distribution, high availability, and improved website or application performance, making it a crucial tool for scaling and reliability.

3- URL Redirection:

Nginx URL redirection allows you to forward web requests from one URL to another, making it handy for managing website changes, enhancing SEO, and ensuring users reach the correct content or pages effortlessly.

4- Caching:

Nginx caching stores frequently accessed web content temporarily, reducing server load and accelerating web page delivery to users, resulting in faster load times and improved website performance.

Lets start the hands on!

First we will create new ubuntu based t2.micro instance, allow port 80, 443 in its security group:

Install the nginx in instance:

Webserver is up and running in our instance:

This webpage is serve from directory:

All the conf file of nginx are available in below directory:

In sites enable if we place any configuration it deployed on webserver but if we put any conf in sites-available it will not deployed, sites-enable is include in nginx.conf file:

Now we will see the default conf of our nginx web server:

By default our nginx server is listening on port 80:

And default directory to server pages is:

We can also change the default port of nginx server in this default file:

Now we will do a hands on project on nginx!

Its a django project which we will route through nginx.

First clone the project code!

To run this project we need docker installed on our instance:

Add ubuntu user to docker group:

Now we will build docker image from docker file in our cloned repo:

Now we will create container from this docker image:

Our application is running on port 8000 now allow port 8000 in SG of instance and check the application status:

But our task is not to expose this port 8000 we will run it through reverse proxy using nginx, so after test remove the allowed port from SG of instance.

As we know we can edit nginx parameters in /etc/nginx/sites-enabled directory, lets do that!

Edit the default file:

Here we told nginx that whenever / comes with the url route it to port 8000:

It will show the empty page now with title only, because we did not put our content in /var/www/html file neither change the path:

Put all the static file of your repo to that html directory:

Now refresh the page:

Still the app is not completely deployed as we are not able to save any notes here, just inspect the page and check the error:

As error shows we are missing /api/notes route, its mean backed is not connected as we only copied the react static files which are use for front end.

First verify is our backed is working or not:

But it is running on local host, so we will also add proxy_pass of this url also:

Now database is also connected.

Now we will run this project through DNS, we will use A record because we are directly mapping to our ip address:

Thats all in this project.

--

--

Ghazanfar Ali
Ghazanfar Ali

Written by Ghazanfar Ali

I write technical blogs on DevOps and AWS, Azure and GCP

Responses (3)