我最近决定放弃 apache2 + mod_wsgi 并尝试使用 gunicorn + nginx 来运行我的 Django 应用程序。
在干净的 Ubuntu 16.04 安装上,我完全没有问题地遵循了这些教程:
本以为一切都会好的,但是在浏览我的网站时,我注意到时不时有几个页面根本不显示,只显示
502 Bad Gateway
错误。望向 error/var/log/nginx/error.log
,我只有如下错误:upstream prematurely closed connection while reading response header from upstream
我不知道它意味着什么:
给你的信息:
/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=myself
Group=www-data
WorkingDirectory=/home/myself/django/myproject
ExecStart=/home/myself/django/myproject/venvprod/bin/gunicorn \
--access-logfile - \
--bind unix:/run/gunicorn.sock \
myproject.wsgi:application
[Install]
WantedBy=multi-user.target
/etc/nginx/sites-enabled/myproject
server {
server_name mysite.fr mysite.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myself/django/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mysite.fr/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mysite.fr/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = mysite.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = mysite.fr) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name mysite.fr mysite.com;
return 404; # managed by Certbot
}
如果还有什么需要,请尽管问!我真正的问题是 我不知道如何找到问题 .我可以使用什么工具?
在此先感谢您的帮助。
编辑 1
我可以确认我的 Django 代码没有任何错误。我按如下方式记录了所有内容:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/home/krazymax/debug-m2g.log',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
'django.template': {
'handlers': ['file'],
'level': 'INFO',
'propagate': True,
},
},
}
并且我在浏览我的网站时没有登录任何错误(页面始终显示,其他页面从不显示,有些有时显示,有时不显示)。
编辑 2
我试着关注 this tutorial ,即不使用中间 socks 文件。没有成功,即使执行
myvenv/bin/gunicorn -c myvenvprod/gunicorn_config.py myproject.wsgi
我可以访问我的网站(不能以其他方式访问):我的页面仍然被随机显示(或不显示)。我真的不知道,这是官方的,这种随意的行为让我发疯!
最佳答案
这个错误通常意味着 Nginx 和 Gunicorn 之间没有“连接”,问题是套接字文件。
运行“/home/myself/django/myproject/venvprod/bin/gunicorn myproject.wsgi -b 0.0.0.0:8000”并查看输出。
这会使用您的 Django 进程执行 gunicorn 服务器,而不会对其进行妖魔化。
可能您的 Django 代码中存在错误并且未正确创建套接字。
当 Gunicorn 打开时,也尝试从浏览器访问 YOUR_IP:8888 (es. 52.45.241.21:8888):您看到该网站了吗?
如果您看到一些 Python 错误,您应该首先使用“manage.py runserver 0.0.0.0:8000”调试您的项目。
runserver 之后的 0.0.0.0:8000 允许您从外部浏览器访问您的站点(如 Gunicorn 中的 -b 0.0.0.0:8000 选项)
请记住:在遵循有关 Django 项目部署的任何教程之前,请使用 runserver 检查项目是否在机器上正常工作
编辑:也尝试一个更简单的教程,如下所示:https://gist.github.com/ravidsrk/8431321
希望这有帮助! :)
关于Django + gunicorn + nginx : 502 bad gateway but sometimes only?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51866884/