502 Bad Gateway Nginx + Flask + Gunicorn(2:没有这样的文件或目录)

时间:2017-10-16 23:55:54

标签: nginx flask ubuntu-14.04 gunicorn proxypass

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

我收到了502 Bad Gateway 的无功/日志/ 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文件,我也不知道为什么我的新手脚本根据教程不会创建一个。非常感谢任何指导。

以下是我的文件:

/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

1 个答案:

答案 0 :(得分:2)

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

写入tmp或选择具有更多权限的用户/组。

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