在ubuntu 16.04上使用nginx、uwsgi python-flASK获取&502错误的网关

2022-06-07 00:00:00 python flask nginx uwsgi ubuntu

问题描述

我正在按照This在Ubuntu 16-04上部署flaskapp(简单的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 t.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

现在我检查了权限(使用ls-l文件名)

-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 /"

我在谷歌上搜索了上面的错误,在过去的两天里读了很多东西,但似乎没有什么对我有用的。 我检查了这些答案,但没有帮助stackanswer-1stackanswer-2,同时我检查了所有数字海洋社区的帖子,但似乎都不起作用。

我完全是服务器新手,对ubuntu了解不多。如果你能帮我找出我做错了什么,或者建议一些更好的教程/方法来部署我的FASK应用程序,那我就太好了。

这些是我的文件

  1. 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')
    
  2. project t.ini

    [uwsgi]
    
    module = wsgi:app
    
    master = true
    
    processes = 5
    
    socket = /home/sajjan/project/project.sock
    
    chmod-socket = 660
    
    vacuum = true
    
    die-on-term = true
    
  3. wsgi.py

    from hello import app
    
    if __name__ == "__main__":
          app.run()
    
  4. 下面是文件:/etc/nginx/ites-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正在运行。

帮帮我,如果你还有什么想知道的,请告诉我。 谢谢

编辑:

我已经创建了一个项目服务文件,它的内容是:

    [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.

当我检查"system ctl状态项目.服务"时

     ● 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当然有点过分,但它是一种快速而肮脏的方式来验证它是否确实是权限问题

相关文章