【问题标题】:Flask App with Gunicorn on Nginx Server Error On POST带有 Gunicorn 的 Flask 应用程序在 Nginx 服务器上 POST 错误
【发布时间】:2018-05-30 19:26:08
【问题描述】:

我是 Python 新手。我托管了 Flask 应用程序,它将在 EC2 实例上的 Nginx 服务器上接受 Gunicorn 的 POST 请求。

当我在路线上发帖时,我收到以下错误:

1578#0: *14 upstream prematurely closed connection while reading response header from upstream, client: myip, server: serverip, request: "POST /train HTTP/1.1", upstream: "http://unix:/home/ec2-user/myproject/myproject.sock:/save_data", host: "serverip"

POST 请求不是来自同一个域,并且总是来自其他域。我需要在我的 nginx.conf 文件中添加一些内容吗?

当我使用命令 python app.py 运行应用程序时,一切正常

【问题讨论】:

    标签: python nginx flask gunicorn


    【解决方案1】:

    我能够解决这个问题。这与我的 NGINX 配置无关(我最初认为是原因)。

    问题出在我的 Gunicorn 配置文件中。

    在我的 Gunicorn 配置文件 (/etc/systemd/system/myproject.service) 中,我将以下内容添加到我的 ExecStart 行:

    --timeout 600

    文件现在看起来像这样:

    [Unit]
    Description=Gunicorn instance to serve myproject
    After=network.target
    
    [Service]
    User=harrison
    Group=www-data
    WorkingDirectory=/home/harrison/myproject
    Environment="PATH=/home/harrison/myproject/myprojectenv/bin"
    ExecStart=/home/harrison/myproject/myprojectenv/bin/gunicorn --workers 3 --timeout 600 --bind unix:myproject.sock -m 007 wsgi:application
    
    [Install]
    WantedBy=multi-user.target
    

    此外,使用python app.py 启动应用程序时没有遇到问题的原因是 Gunicorn 没有以这种方式提供服务……它使用的是 Flask 测试开发服务器。开发服务器的超时时间与 Gunicorn 不同。默认情况下,我相信 Gunicorn 超时默认为 30 秒。就我的申请而言,这太低了。

    【讨论】:

    • 是的!你救了我的命……非常感谢。我有谷歌的每一页,从明天开始就坚持这个。我创建了 8 个 EC2 实例并更改了不应更改的内容。
    猜你喜欢
    • 2019-10-03
    • 2014-03-08
    • 2017-01-07
    • 2020-12-14
    • 2020-01-10
    • 1970-01-01
    • 2021-09-16
    • 2017-02-14
    • 1970-01-01
    相关资源
    最近更新 更多