NginX + uWSGI Configuration – Live Learning Notes

In the first look, NginX configurations seems a bit self explanatory. But we must understand these configs and their effects in depth, in order to avoid those unwanted behaviour we come across NginX due to our lack of knowledge and the habit of taking configs for granted. This post is mere a note making that I did today while learning NginX to set it up as a reverse-proxy to uWSGI of my DRF test project.

disclaimer.init()

As I am writing this post, I don’t know much about NginX + uWSGI configs. Today, I’ll start reading through various internet resources and would keep updating this post as my notes. That’s it! If you want a more professional explanations, then this might not be the place. But it might help someone else. Also, as these are just notes, my writing could be rough to follow. If you understand the risks, go ahead!

notes.begins()

#1 NginX Configs – Intro

Let’s get introduced with NginX configs first. #1: Managing Configuration Files

#2 NginX Configs – Verbose

Now I got an idea of what NginX config file contains. Let’s go a bit deeper in their structural understanding and best practices. #2: Understanding the nginx configuration file structure

#3 How to add your custom configs?

After reading #2, my perspective for nginx configs is surely bigger now. But everything was like explaining the defaults. But every time when we use nginx for a project, we would use different configurations per project. How to handle those? How to include your custom configurations without modifying the default NginX file? For getting started with these custom configs, here is a nice post. #3: How to configure NginX

From #3, I got it that we can simply add our custom config file at /etc/nginx/sites-available and then adding its symlink to /etc/nginx/sites-enabled (as the test box I am using is of ubuntu20.04) and I have verified that my default /etc/nginx/nginx.conf contains this directive include /etc/nginx/sites-enabled/*; in its http event’s context. So I am on the right path. 🙂

#4 Writing custom NginX config file

Now, I just need to write a config file that will make NginX to work as a reverse proxy for my uWSGI server of DRF project. Let me find how to do that. Going for this one. #4: How to deploy Python WSGI application using uWSGI server with NginX

1 Hour Later

Damn! I didn’t know that I had to first setup WSGI configuration to achieve it. So I was trying to setup uWSGI first and ran it. Soon I killed it with CTRL+C. Did this thing around 2-3 times after changing configuration parameters a bit. Now my server is hanged! Have spent about an hour to resolve the issue but the CLI is just stuck.

30 Mins Later

Nothing worked. But I don’t know how the server is now back to normal :shrug: I tried htop, ps, to find out which process hanging the server up. But these commands were not responding. I also tried to sudo su and got no response either. I also tried free -h to see if the RAM is over flowing, and same, got no response. But now everything is fine.

#5 and #6 Setting up uWSGI + NginX configs

Let’s continue then. After getting to that fix, I read through the #4. It was looking a bit more complex, so I wanted an easy to follow tutorial for this. And I chose this lovely one #5 Django uWSGI NginX Postgresql Setup With Python. And this one. #6: How to set up uWSGI and NginX to serve Python apps

errors.log()

I faced issues while directing Nginx to uWSGI server. So here are some tips and main pointers that I referenced.

final.results()

At last my final working NginX configs are really small, but working!

server {
    listen 80;
    server_name public_ip_address;

    location / {
        uwsgi_pass unix:/tmp/uwsgi-my-project.sock;
        include uwsgi_params;
        uwsgi_read_timeout 300s;
        uwsgi_send_timeout 300s;
    }

    access_log /path/to/any_directory/dev-nginx-access.log;
    error_log /path/to/any_directory/dev-nginx-error.log;
}

And the corresponding uWSGI configs are as follows:

[uwsgi]
module = wsgi:application
master = true
harakiri = 60
max-requests = 1000
socket = /tmp/uwsgi-my-project.sock
chmod-socket = 666
logto = /path/to/any_directory/dev-uwsgi.log

And I created that service file for uWSGI server so that I can manage it just like we do for Nginx. Like service nginx start etc. So here is the service file:

[Unit]
Description=uWSGI instance to serve my test project on server
After=network.target

[Service]
WorkingDirectory=/home/ubuntu/path/to/project/root/
Environment="PATH=/home/ubuntu/path/to/venv/bin"
ExecStart=/home/ubuntu/path/to/venv/bin/uwsgi --ini /home/ubuntu/path/to/mywsgi.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Saved this file as “mywsgi.service” at “/etc/systemd/system/”. Now tried service mywsgi status . It showed the expected. Now I could start, stop, restart, etc on this uWSGI service file.

resources.dump()

  1. https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/
  2. https://www.digitalocean.com/community/tutorials/understanding-the-nginx-configuration-file-structure-and-configuration-contexts
  3. https://www.linode.com/docs/guides/how-to-configure-nginx/
  4. https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-applications-using-uwsgi-web-server-with-nginx
  5. https://www.freecodecamp.org/news/django-uwsgi-nginx-postgresql-setup-on-aws-ec2-ubuntu16-04-with-python-3-6-6c58698ae9d3/
  6. https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-ubuntu-14-04
  7. https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/

post.close()

Total time taken to write this post live == 7.5 Hrs (believe it or not -_- )

Please feel free to add more helpful resources to learn NginX and uWSGI configs in the comment section below. Hope it might help someone.

Hail NginX!
sudo service nginx stop

One thought on “NginX + uWSGI Configuration – Live Learning Notes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s