'Using Nginx with gunicorn without .sock on linux?

I have a python web app that uses Flask and gunicorn, and everything works great. I plan on hosting this on a public website and want to use nginx, but did not set up the app to use nginx originally. A digital oceans tutorial I found on the topic suggests creating a .sock file for nginx to bind to. Instead, I start gunicorn using gunicorn wsgi:app.

In /etc/nginx/sites/available/default I reverse proxy incoming traffic to localhost, port 8000 where gunicorn is running. This works but I have a few concerns:

  • I don't think nginx is working as intended with gunicorn, but I'm not sure how to check.
  • I can see the IP instead of the domain when I connect to the website.
  • I run it on localhost during development and the public IP during production, and don't want to impede development.

If it's relevant, I'm hosting the web app on an Ubuntu server that I have locally. Again, everthing seems to works but I have a feeling I'm horribly misusing nginx. I'm really just looking to know if I should be using a socket with gunicorn/nginx. Thanks.



Solution 1:[1]

What I can recommend is to:

  1. Make sure that you have Nginx properly set up and vhosts created. ( https://hostadvice.com/how-to/how-to-setup-the-nginx-web-server-with-nginx-server-blocks-on-ubuntu-18-04/ )

  2. Start your application on a specific port.

  3. Once you have it running, add the following code snippet to the Nginx vhost:

    location / {

     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $host;
     proxy_pass http://127.0.0.1:3000;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
    

    }

Replacing "3000" with the port of your own app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1