'Flask API via Nginx/Gunicorn on DigitalOcean

I'm trying to set up my first Flask API via gunicorn/nginx on a droplet. I've had issues with nginx in the past and I thought I had gotten it fairly well figured out. Below is the relevant section of my nginx.conf

server {
    listen 80;                                                                                                                                                                                                                                                                                                                                                                                                                    
    location / {
        include proxy_params;
        proxy_pass http://unix:/root/path/to/app.sock;
    }
}

I can curl or visit the IP address or even my subdomain and get the nginx home page no problem. The issue is when I try to use port 5000 which is the default for Flask. I get a "connection refused" error no matter how I try it. I've got UFW allowing "Nginx HTTP", "Nginx Full", and even port 5000 specifically. I'm not even able to curl localhost:5000 on my droplet without a connection refused error.

api.service file:

[Unit]
Description=flask_app gunicorn service
[Service]
User=root
WorkingDirectory=/root/path/to
ExecStart=gunicorn --workers 5 --bind unix:/root/path/to/app.sock -m 777 flask_app:app
Restart=always
[Install]
WantedBy=multi-user.target

What am I missing? For reference, I had it functioning last night and was testing my application with no issues; the application connected to the API and ran my Python functions just fine. I've not changed any settings since then, I hadn't even rebooted my droplet.

EDIT:

I changed port 80 in my nginx settings to port 5000, and I'm now able to curl my subdomain and my IP address from my home server and get the nginx home page. However, I'm now getting a 502 bad gateway on port 5000 both on my droplet and from my home server.



Solution 1:[1]

In this instance, my problem was in my nginx.conf. For some reason by default, my droplet had a couple dozen different user profiles set up, and used one of those for my nginx.conf file. Even after a sudo chown -R www-data:www-data /var/lib/nginx it still wasn't working, so I set my nginx.conf to root and now we're off to the races.

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 Devin Gardner