High Availability with NginX

Muhammad Oktoluqman Fakhrianto
3 min readDec 2, 2019

--

Hewwo, UwU, my nawme iws Oktow, I’m going tuwu teww my stowy own setting up high avaiwabiwity own my wocaw django sewvew.

Okay I will stop there.

The steps shown in this tutorial is for Ubuntu Linux, as It is my main Operating System. The steps for Windows or other Linux distros may be different, but the configuration file should work for you just fine.

Step 1: Install NginX

To install NginX, all you have to do is type:

sudo apt install nginx

I didn’t encounter any problems when installing NginX.

Step 2: Edit The Configuration File

You need to edit some configuration files to set up the load balancer. The file you need to edit is /etc/nginx/sites-enabled/default and make it look like so:

Note: this requires root access. You may want to save the default configuration file before editing, just in case.

This was not my first solution, The first thing I tried is to follow a tutorial on Setting up Django and your web server with uWSGI and nginx. I wasn’t so sure to follow the tutorial because it wasn’t as simple as how Mr. Adin showed me and other students. While following that tutorial, I met a problem when installing uwsgi.

So naturally I asked my friend Dipta how he did it. It turned out quite easy, and without installing uwsgi. He told me to create a new file /etc/nginx/conf.d/nginx.conf with configuration similar to above and delete /etc/nginx/sites-enabled/default because it won’t work without deleting the file. What I did instead is if move the content of nginx.conf to default and it also works.

Step 3: Restart NginX

Once you’re done editing, you need to restart NginX before the changes are applied. To restart NginX, type:

sudo service nginx restart

If an error pops up after you restart NginX, you should look at the configuration file and see if you have any typos or missing braces ‘{}’.

Step 4: Is It Working?

To test if NginX is working correctly, I ran 2 django server. For the first one I ran it on port 8000 by typing:

python manage.py runserver localhost:8000

And for the second one I ran it on port 9000 by typing:

python manage.py runserver localhost:9000

Now, you can open the browser and access the load balancer at http://localhost. It should open normally like your django website. But the requests in both terminal should be distributed.

Running port 8000 (left) and port 9000 (right)

If I stop one of the server, for example the server with port 8000 is stopped using CTRL + C, The main website should still be working because all requests is server by the server with port 9000.

Stopped port 8000 (left) and running port 9000 (right)

If I stop both server, NginX should display an error page because there aren’t any server that can fulfill the request.

Stopped port 8000 (left) and port 9000 (right)

Conclusion

Finally it’s done! You now have a basic setup for high availability server with NginX for your computer. There are other tools for high availability setup like this, with Apache.

This is my story. What’s yours?

--

--

No responses yet