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

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

我正在尝试根据本教程将我的 Flask 应用程序连接到 Nginx 和 Gunicorn:How To Serve Flask Applications with Gunicorn and Nginx on Ubuntu 14.04

我收到 502 错误网关 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>"

Nginx 似乎找不到 myproject.sock 文件,我不知道为什么我的 upstart 脚本不会根据教程创建一个。非常感谢任何指导。

以下是我的文件:

/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

注意:我 运行 下面文件中的 cd 和 exec 命令用于测试目的,它工作正常。

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 这是已启用站点的符号链接

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

我采取的调试步骤:

(1) 我查了一下upstart脚本是运行

$ sudo status myproject
myproject start/running, process 22476

(2) Nginx 是 运行

(3) 奇怪,我没看到我的 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

[已解决]我的一位导师指出了这一点。
www-data/www-data 无法写入 /home/myproject/

写入 tmp 或选择具有更多权限的 user/group。

我选择写信给/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

Nginx 文件如下所示:

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