NGINX configuration set up (with uWSGI)

7th June, 2021

Once a uWSGI server is running, one may prefer to have Nginx as the reverse proxy to the uWSGI server. This set up is quite commonly used for example while deploying a Django project to production.

Here a simple configuration file has been added which can quickly set up the NGINX and uWSGI set up working. Just follow the next steps.

Note: the following steps have been tried out in Linux operating systems such as Ubuntu in this case.

 

  • Initiate a uWSGI server listening to port 5000 (as an example port to be used for the following steps. Do not use port 80 or 443 or any other standard ports)
  • Install Nginx
  • Create a new file named "mywebsite" in the sites-available folder (/etc/nginx/sites-available)
  • Insert the following content in the mywebsite file.

    server {
        listen 80;
        server_name <domain/ip>;
    
        location /static {
            alias /path_to_staticfiles; # your project's static files location - amend as required
        }
        # Finally, send all non-media requests to the server.
        location / {
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Proto $scheme;
            proxy_read_timeout 300;
            proxy_connect_timeout 300;
            proxy_pass http://0.0.0.0:5000;  # Add your own server port here instead of 5000.
            include     uwsgi_params; # the uwsgi_params in nginx
        }
    }
    
    server {
        listen 80;
        server_name yourdomain.com;
    
        return 301 http://www.yourdomain.com$request_uri;
    }​
  • Run the following command
    sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled

  • Restart Nginx
    sudo /etc/init.d/nginx restart

  • That's it. You may be able to view the website now at standard http port 80.