在ubuntu 16.04上使用nginx,uwsgi python-flask获取“502 Bad Gateway”

我正在按照这个在Ubuntu 16-04上部署一个烧瓶应用程序(简单的hello world). digital Ocean tutorial

一切正常,直到测试uWSGI服务.之后我按照描述的步骤进行操作,当我最终到达底部并检查服务器IP地址时,我得到了:

502 Bad Gateway

好的.我搜索并检查了我的错误日志,我得到了: –

2017/01/16 05:29:27 [crit] 20714#20714: *2 connect() to unix:/home/sajjan/project/project.sock failed (2: No such file or directory) while connecting to upstream, client: xx.9.xxx.xxx, server: 138.xxx.xx.xxx, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: "xx.xx.xx.xx"

因此,在获取错误日志后,我手动创建了project.sock文件.再次转到服务器IP地址然后相同的错误“502 Bad Gateway”

再次检查错误日志并找到了这个

2017/01/16 06:07:11 [crit] 20874#20874: *1 connect() to unix:/home/sajjan/project/project.sock failed (13: Permission denied) while connecting to upstream, client: 47.9.237.113, server: XX.XX.XX.XX, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: " XX.XX.XX.XX "

我想出了权限问题并使用以下命令更改权限

sudo chmod 666 project.sock

现在我检查了permision(使用ls -l filename)

-rw-rw-rw- 1 root root 0 Jan 16 05:31 project.sock

现在我回去查看服务器的IP,但发现了相同的“502 Bad Gateway”.
我再次检查了错误日志,发现了这个:

017/01/16 06:13:31 [error] 20897#20897: *6 connect() to unix:/home/sajjan/project/project.sock failed (111: Connection refused) while connecting to upstream, client: 47.9.237.113, server: XX.XX.XX.XX, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: " XX.XX.XX.XX ", referrer: "http:// XX.XX.XX.XX /"

我用Google搜索了上面的错误,在过去的两天里看了很多,但似乎没什么可以帮我的.
我已经检查了这些答案,但没有帮助stackanswer-1 stackanswer-2以及这些我检查了所有数字海洋社区线程,但似乎没有任何工作.

我完全是服务器的开端,对ubuntu不太了解.如果你能帮助我找出我做错了什么或建议一些更好的教程/方法来部署我的烧瓶应用程序,那么我会很高兴.

这些是我的文件

> hello.py

from flask import Flask
app = Flask(__name__)

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

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

> project.ini

[uwsgi]

module = wsgi:app

master = true

processes = 5

socket = /home/sajjan/project/project.sock

chmod-socket = 660

vacuum = true

die-on-term = true

> wsgi.py

from hello import app

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

>下面是文件:/ etc / nginx / sites-available / project

server {
    listen 80;
    server_name 138.197.28.107;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/sajjan/project/project.sock;
    }
}

当我运行命令时:

    sudo service uwsgi restart

输出:

   Failed to restart wsgi.service: Unit wsgi.service not found. 

而输出

   sudo service nginx status/restart

然后这表明nginx正在运行.

帮助我,如果您想知道其他任何事情,请告诉我.
谢谢

编辑:

我创建了一个project.service文件,其内容是:

    [Unit]
    Description=uWSGI instance to serve project
    After=network.target

    [Service]
    User=sajjan
    Group=www-data
    WorkingDirectory=/home/sajjan/project
    Environment="PATH=/home/sajjan/project/venv/bin"
    ExecStart=/home/sajjan/project/venv/bin/uwsgi --ini project.ini

    [Install]
    WantedBy=multi-user.target

我想我必须在命令下运行:

    sudo systemctl start project

输出是:

    Warning: project.service changed on disk. Run 'systemctl daemon-reload' to reload units.

当我跑

     sudo systemcl reload project 

然后输出:

    Failed to reload project.service: Job type reload is not applicable for unit project.service.
    See system logs and 'systemctl status project.service' for details.

当我检查“systemctl status project.service”时

     ● project.service - uWSGI instance to serve project
      Loaded: loaded (/etc/systemd/system/project.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2017-01-16 17:49:29 UTC; 6min ago
      Main PID: 27157 (code=exited, status=203/EXEC)

     Jan 16 17:49:29 learningwithpython systemd[1]: Started uWSGI instance to serve project.
     Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Main process exited, code=exited, status=203/EXEC
     Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Unit entered failed state.
     Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Failed with result 'exit-code'.
最佳答案
我使用指南遇到了同样的问题.据我读过; 502坏网关是Nginx无法正确连接到uwsgi的症状.更改套接字的权限解决了我的问题.

sudo chmod 777 /home/sajjan/project/project.sock
sudo systemctl restart nginx

777的情况有点过分,但它是一种快速而肮脏的方式来验证它是否实际上是权限问题

转载注明原文:在ubuntu 16.04上使用nginx,uwsgi python-flask获取“502 Bad Gateway” - 代码日志