问题
当我尝试通过 gunicorn 执行 django 管理命令时收到 502 bad gateway
逻辑线
我认为问题出在权限上,像gunicorn这样的东西不能调用命令。我这么说是因为我可以在不使用 gunicorn 的本地运行它。
我可以用这两种方法运行它:
python manage.py runserver
之后,使用 Postman 启动它就可以了。第二个是通过终端呼叫
python manage.py command_name
,这也可以。在生产中,我也可以使用
python manage.py command_name
. 但不是邮递员,因为它返回 502(主要问题)
PS。如果我删除call_command
它返回 200 ok,那么,核心问题似乎是这个命令的执行。
编码
class TestCommandView(views.APIView):
def post(self, request):
id = request.data['id']
try:
call_command('command_name', target_id=id)
return Response({"status": "success"})
except Exception as error:
return Response({"status": "error: " + str(error) })
返回样品
<html>
<head>
<title>502 Bad Gateway</title>
</head>
<body bgcolor="white">
<center>
<h1>502 Bad Gateway</h1>
</center>
<hr>
<center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
独角兽会议
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
RuntimeDirectory=gunicorn
WorkingDirectory=/var/www/project
ExecStart=/var/www/project/venv/bin/ddtrace-run /var/www/project/venv/bin/guni$
Environment="DJANGO_SETTINGS_MODULE=project.settings.prod"
[Install]
WantedBy=multi-user.target
Nginx 日志错误
2019/03/13 13:43:38 [error] 27552#27552: *3128 upstream prematurely closed connection while reading response header from upstream, client: IP, server: api.project.com, request: "POST /api/project/endpoint/ HTTP/1.1", upstream: "http://unix:/tmp/project.sock:/api/project/endpoint/", host: "api.project.com"
我试过的
sudo chown -R www-data:www-data /var/www/project
sudo chown -R ubuntu:ubuntu /var/www/project
- 根据这个问题解决方案更改我在 gunicorn 配置上的 Environment 值:Django call_command permissions nginx+gunicorn+supervisord。添加PYTHONPATH,但是这个人在主管配置上使用它,这个项目不使用主管,所以我试图把它放在gunicorn文件中,这只是一个尝试。