首页 文章

在发布Nginx服务器错误时使用Gunicorn的Flask应用程序

提问于
浏览
2

我是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运行app时,一切正常

1 回答

  • 2

    我能够解决这个问题 . 这与我的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秒 . 就我的申请而言,这太低了 .

相关问题