0
votes

I'm having trouble getting gunicorn and Nginx to work together and allow me to offer a simple API via flask:

Locally, running gunicorn and getting responses from the server works fine:

gunicorn wsgi:app (start server)
[2019-06-11 23:12:48 +0000] [14615] [INFO] Starting gunicorn 19.9.0
[2019-06-11 23:12:48 +0000] [14615] [INFO] Listening at: http://127.0.0.1:8000 (14615)
[2019-06-11 23:12:48 +0000] [14615] [INFO] Using worker: sync
[2019-06-11 23:12:48 +0000] [14619] [INFO] Booting worker with pid: 14619


curl http://127.0.0.1:8000/predict (client call server for prediction)
output: "SERVER WORKS"

The problem arises when I try to use Nginx as well.

/etc/systemd/system/app.service

[Unit]
Description=Gunicorn instance to serve app
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/root/server
ExecStart=/usr/local/bin/gunicorn --bind unix:app.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/app

server {
    listen 80;
    server_name [SERVER_IP_ADDRESS];

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

}

The status of my systemd looks fine:

systemctl status app
● app.service - Gunicorn instance to serve app
   Loaded: loaded (/etc/systemd/system/app.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-06-11 23:24:07 UTC; 1s ago
 Main PID: 14664 (gunicorn)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/app.service
           ├─14664 /usr/bin/python /usr/local/bin/gunicorn --bind unix:app.sock -m 007 wsgi:app
           └─14681 /usr/bin/python /usr/local/bin/gunicorn --bind unix:app.sock -m 007 wsgi:app

systemd[1]: Started Gunicorn instance to serve app.
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14664] [INFO] Starting gunicorn 19.9.0
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14664] [INFO] Listening at: unix:app.sock (14664)
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14664] [INFO] Using worker: sync
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14681] [INFO] Booting worker with pid: 14681

When I make a request to the server, I have trouble connecting:

curl http://[SERVER_IP_ADDRESS]:80/predict

<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>

EDIT:

I tried removing server_name [SERVER_IP_ADDRESS]; from /etc/nginx/sites-available/app. I now receive 'Welcome to nginx!' at http://SERVER_IP_ADDRESS, and '404 Not Found' at http://SERVER_IP_ADDRESS/predict

FYI, my flask app only has one route, which is '/predict'

1
Is port 80 open on the host machine? - Braden Holt
@BradenHolt yes it is open now, I now get '502 Bad Gateway' instead - echan00
502 Bad Gateway is probably due to an application error (meaning your flask application). Can you check your Gunicorn logs? sudo journalctl -n 100 -u gunicorn may work depending on your os or check your sys log. - Braden Holt

1 Answers

0
votes

It looks like you don't have Port 80 open, so here's a quick iptables command to do so:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT