LLL
LLL

Reputation: 117

502 Bad Gateway Nginx+Flask+Gunicorn (2: No such file or directory)

I am trying to connect my flask app to Nginx and Gunicorn, based on this tutorial: How To Serve Flask Applications with Gunicorn and Nginx on Ubuntu 14.04.

I am getting a 502 Bad Gateway var/log/nginx

2017/10/16 21:17:04 [crit] 11284#0: *8 connect() to unix:/home/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: <myIP>, server: <myIP>, request: "GET / HTTP/1.1", upstream: "http://unix:/home/myproject/myproject.sock:/", host: "<myIP>"

It seems like Nginx couldn't find the myproject.sock file, and I don't know why my upstart script wouldn't create one based on the tutorial. Any guidance is greatly appreciated.

Below are my files:

/home/myproject/myproject.py

from flask import Flask
application = Flask(__name__)

@application.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    application.run(host='0.0.0.0')

/home/myproject/wsgi.py

from myproject import application

if __name__ == "__main__":
    application.run()

/etc/init/myproject.conf

note: I ran the cd and exec command in the file below for testing purposes and it works fine.

description "Gunicorn application server running myproject"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid www-data
setgid www-data

script
        cd /home/myproject
        exec gunicorn --bind unix:myproject.sock -m 007 wsgi
end script

/etc/nginx/sites-available this is symlinked to sites-enabled

server {
    listen 80;
    server_name <myIPaddressHere>;
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/myproject/myproject.sock;
    }
}

Debugging Steps I took:

(1) I checked that the upstart script is running

$ sudo status myproject
myproject start/running, process 22476

(2) Nginx is running

(3) Weird, I don't see my myproject.sock

# netstat -lpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11279/nginx
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1304/sshd
tcp6       0      0 :::80                   :::*                    LISTEN      11279/nginx
tcp6       0      0 :::22                   :::*                    LISTEN      1304/sshd
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     SEQPACKET  LISTENING     7190     386/systemd-udevd   /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     8774     1120/acpid          /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     6541     1/init              @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     8339     859/dbus-daemon     /var/run/dbus/system_bus_socket

Upvotes: 0

Views: 3969

Answers (1)

LLL
LLL

Reputation: 117

[Solved] A mentor of mine pointed this out.
www-data/www-data can not write to /home/myproject/

Either write into tmp or choose a user/group that has more permissions.

I chose to write to /tmp

description "Gunicorn application server running myproject"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid www-data
setgid www-data

script
        cd /home/myproject
        exec gunicorn --bind unix:/tmp/myproject.sock -m 007 wsgi
end script

And the Nginx file looks like this:

server {
    listen 80;
    server_name <ip>;
    location / {
        include proxy_params;
        proxy_pass http://unix:/tmp/myproject.sock;
    }
}

Upvotes: 5

Related Questions