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

Getting quot;502 Bad Gatewayquot; with nginx, uwsgi python-flask on ubuntu 16.04(在ubuntu 16.04上使用nginx、uwsgi python-flASK获取502错误的网关)
本文介绍了在ubuntu 16.04上使用nginx、uwsgi python-flASK获取&502错误的网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于在ubuntu 16.04上使用nginx、uwsgi python-flASK获取&502错误的网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Set up virtualenv with Paramiko SSH(使用Paramiko SSH设置Virtualenv)
GLib.Error: Required gtk+ version 3.20, current version is 3.18 (7)(GLib.Error:必需的GTK+版本3.20,当前版本为3.18(7))
What is the process quot;python3 unattended upgrade shutdownquot;?(Python3无人参与升级关闭的过程是什么?)
Weasyprint get undefined property at invoking write_pdf: quot;AttributeError: #39;PosixPath#39; object has no attribute #39;read_text#39;quot;(Weasyprint在调用WRITE_pdf:Quot;AttributeError:#39;PosiPath#39;对象没有属性#39;Read_Text#39;时获取未定义的属性) - IT屋-程序员软件开发技术分
Could not load dynamic library #39;libcupti.so.11.0#39;; dlerror: libcupti.so.11.0: cannot open shared object file(无法加载动态库#39;libcuti.so.11.0#39;;dlerror:libcuti.so.11.0:无法打开共享对象文件)
Could not build wheels since package wheel is not installed(由于未安装套装轮,因此无法生成控制盘)