一段时间以来,我一直在尝试解决生产站点上的500和502错误的网关问题。我查看了nginx日志文件,发现那里有错误,但似乎找不到任何帮助。这是nginx的错误日志:
2019/04/28 20:52:10 [crit] 921#921: *1 connect() to unix:/pathto/django/project/projectname.sock failed (2: No such file or directory) while connecting to upstream, client: x.x.x.x, server: example.com, request: "GET / HTTP/1.1", upstream: "http://unix:/pathto/django/project/projectname.sock:/", host: "example.com"
2019/04/28 20:53:55 [error] 921#921: *17 upstream prematurely closed connection while reading response header from upstream, client: x.x.x.x, server: example.com, request: "POST /games/ HTTP/1.1", upstream: "http://unix:/pathto/django/project/projectname.sock:/x/", host: "example.com", referrer: "http://example.com/x/"
2019/04/28 20:55:57 [error] 921#921: *17 upstream prematurely closed connection while reading response header from upstream, client: x.x.x.x, server: example.com, request: "POST /games/ HTTP/1.1", upstream: "http://unix:/pathto/django/project/projectname.sock:/x/", host: "example.com", referrer: "http://example.com/x/"
我尝试增加Nginx和Gunicorn的超时时间,但是没有运气。由于多个API请求,我网站上的某些操作需要几分钟才能完成,我相信可能是时候了,但是我不确定。
我的网站可用的nginx文件:
server {
listen 80;
server_name example.com;
location = /favicon.ico {
return 204;
access_log off;
log_not_found off;
}
location /static/ {
root /path/to/static/file;
}
location / {
include proxy_params;
proxy_pass
http://unix:/pathto/django/project/projectname.sock;
}
}
我的gunicorn起始文件:
#!/bin/bash
NAME="projectname"
DIR=/path/to/project/root
USER=u
GROUP=u
WORKERS=3
BIND=unix:/home/u/run/gunicorn.sock
DJANGO_SETTINGS_MODULE=projectname.settings
DJANGO_WSGI_MODULE=projectname.wsgi
LOG_LEVEL=error
cd $DIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DIR:$PYTHONPATH
exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $WORKERS \
--user=$USER \
--group=$GROUP \
--bind=$BIND \
--log-level=$LOG_LEVEL \
--log-file=-
如上所述,我的网站开始执行api操作时遇到了500多个错误和502个错误。这些操作涉及(在某些情况下)获取大量api数据并将其存储到我的项目模型中。在许多情况下,它将在操作进行约30秒后502。很多次,我第二次重试后,它可以按预期正常工作。此刻我很困,不胜感激,谢谢!
此外,如果有帮助,我使用了本指南来部署该项目:https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04