What should you do to prevent the engine broke up on your website?

this is a tutorial that you need to keep your website runs well using Nginx — “Engine X”.

source: google.com

First, you need to know about the Load Balancer? Lemme give you an illustration :

“ What would happen if your company/campus make a new parking system where there is only one way to in and out of it? ”

source: https://kumparan.com/kumparannews/ui-secure-parking-nasibmu-kini-1rTaVol00dd

The answer is quite predictable, it will make traffic jams in rush hour. If the request is a lot more than the throughput, the parking operator wouldn’t able to manage the parking system well and wasting time for the road-user.

One of the solutions to solve this problem is by adding more operators to it. At first, the operator only can do the job one at a time. After you add more operators, the job will be finished faster. — Your website likes the company/campus. If there are a lot of users trying to access your website at the same time and your website can’t handle it, there is only one thing happen; your website gets down or hard to access. You can add more machines or servers to solve the problems but how the request can divide well to each machine?

Here load balancer is needed. Load balance helps with distributing the request well to each machine/server so the work of the machines is effective and the website would run well.

Now, we already know what is load balancer and the role of it. Let us start the interesting part,

How we use the load balancer on our website?

here we want to connect the load balancer with our web server. In this case, I’m using Django as my web server and we will try to use Nginx to manage our web servers. We need to install Nginx first, here the steps ;

  1. First, we need to install Nginx stable version here (windows). After that, extract it into your django-project folder.
place it at the same folder with the django-project

2. After that, let start the Nginx. Open the command prompt on the Nginx folder directory then type “start nginx”.

3. To verify that the Nginx already started. In your website browser open “localhost”. Your web page will show the image below.

localhost page with nginx

If you want to know active session in your Nginx type “ tasklist /fi “imagename eq nginx.exe” “ in your command prompt. If the command prompt doesn’t show the image below, get back to step 2.

4. To turn off the Nginx type "nginx -s -stop”. (In a few cases, Nginx can’t be to stop (still running), you need to turn it off manual by using Task Manager).

We already install and know how to use Nginx. So, let connecting the Nginx with our Django-project !!!

  1. Let us run our Django server in two ports, in this cases we use port:8000 and port:9000.
  • First, we open two command prompts, and run two Django server at the same time. “python manage.py runserver 8000” and “python manage.py runserver 9000”.
  • After that, run our Nginx in the other command prompt and type “start nginx
  • Then open the URL “localhost” in our web browser. We found that the page doesn’t change. This means the Nginx isn’t connected to our web servers.

2. Calm down and don’t be panic — you almost done. This happens because we haven’t set Nginx to be connected to the Django-project. Follow these path :

  • Open folder nginx-1.16.1 that we extracted before, and open folder conf.
  • Backup nginx.conf in case we will change the content of the file.
  • After backup the file, open file nginx.conf in your text editor (not the backup one). We found the initial content of the file like this.
the initial content of nginx.conf
  • Delete all the content of the file and replace it with this code
events {
worker_connections 1024;
}

http {
upstream localhost {
server localhost:8000;
server localhost:9000;

# add other port here
# you also can add weigh too here

# server localhost:8000 weight = 3;
}

server {
listen 80;
location / {
proxy_pass http://localhost;
}
}
}
  • After that save the file.

3. Open back the command prompt that running Nginx, then type “nginx -s reload” to reload the new content of nginx.conf file.

4. Reload localhost in our web browser. If the browser success changing into the page that showed in our Django-server, so Nginx already connected to our Django-projects.

That’s all Fellas.

Thank you for reading my articles, hope you enjoy it. Keep learning and never give up. If you have a question about the steps free to reach me on my twitter. See you in the next article !!!

Computer Science Enthusiast