Flask - Gunicorn在卷曲后返回404到同一个实例

时间:2017-07-11 15:25:20

标签: python ubuntu flask gunicorn

我正在尝试使用Gunicorn部署我的Flask网站。这个repo显示了我开始使用的代码,我添加了自己的控制器等。

我遇到的问题如下:

  1. python manage.py runsever在本地工作正常
  2. sudo gunicorn flask_application:app -b 127.0.0.1:8000在我的服务器上正常运行应用程序。
  3. Gunicorn日志显示:

    [2017-07-11 15:06:38 +0000] [3926] [INFO] Starting gunicorn 19.1.1
    [2017-07-11 15:06:38 +0000] [3926] [DEBUG] Arbiter booted
    [2017-07-11 15:06:38 +0000] [3926] [INFO] Listening at: http://127.0.0.1:8000 (3926)
    [2017-07-11 15:06:38 +0000] [3926] [INFO] Using worker: sync
    [2017-07-11 15:06:38 +0000] [3931] [INFO] Booting worker with pid: 3931
    [2017-07-11 15:06:38 +0000] [3932] [INFO] Booting worker with pid: 3932
    [2017-07-11 15:06:38 +0000] [3937] [INFO] Booting worker with pid: 3937
    [2017-07-11 15:06:39 +0000] [3926] [DEBUG] 3 workers
    [2017-07-11 15:06:42 +0000] [3932] [DEBUG] GET /profile:8000
    [2017-07-11 15:06:47 +0000] [3937] [DEBUG] GET /profile
    [2017-07-11 15:06:49 +0000] [3932] [DEBUG] GET /profile:8000
    

    正如您所看到的,我的curl 127.0.0.1/profile:8000(在同一个方框上)请求通过,日志中没有抛出任何错误,但是转发显示:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <title>404 Not Found</title>
    <h1>Not Found</h1>
    <p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>
    

    Gunicorn配置:

    workers = 3
    errorlog = '/home/www/flask_project/logs/gunicorn-error.log'
    accesslog = '/home/www/flask_project/logs/gunicorn-access.log'
    loglevel = 'debug'
    

    这个question与我的设置类似,但我很高兴直接使用gunicorn而不是通过Flask-Script运行。

    我猜:

    我的应用程序的结构可以在上面的repo链接中看到,但简而言之,flask_applicaiton.__init__.py文件包含app。当我运行gunicorn命令时,似乎没有导入视图。

    话虽如此,蓝图的导入和注册如下:

    flask_applicaiton.__init__.py

    # Business Logic
    # http://flask.pocoo.org/docs/patterns/packages/
    # http://flask.pocoo.org/docs/blueprints/
    from flask_application.public.controllers import public
    app.register_blueprint(public)
    
    from flask_application.users.controllers import users
    app.register_blueprint(users)
    
    from flask_application.admin.controllers import admin
    app.register_blueprint(admin)
    

    仅供参考 - 一旦我使用Gunicorn正确运行应用程序,我将使用nginx将代理转换为我的应用程序。目前,nginx向我展示:

    2017/07/11 15:23:35 [error] 1204#1204: *204 connect() failed (111: Connection refused) while connecting to upstream
    

    我认为是因为Gunicorn没有正确地为Flask应用程序提供服务。

    有关原因或如何调试此内容的任何建议都将非常感激。

1 个答案:

答案 0 :(得分:0)

尝试在虚拟环境中使用gunicorn和flask。而不是:

sudo gunicorn flask_application:app -b 127.0.0.1:8000

首先设置虚拟环境,激活它。比pip install gunicorn。还要在没有sudo的情况下在venv中安装烧瓶和所有其他东西。

您可以查看我的repository