我是 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 运行应用程序时一切正常
请您参考如下方法:
我能够解决这个问题。这与我的 NGINX 配置无关(我最初认为这是原因)。
问题出在我的 Gunicorn 配置文件中。
在我的 Gunicorn 配置文件 (/etc/systemd/system/myproject.service) 中,我将以下内容添加到我的 ExecStart 行:
--超时 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 秒。就我的申请而言,这太低了。
