[英]Django + gunicorn + nginx: 502 bad gateway but sometimes only?
我最近決定放棄 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
我不知道它意味着什么:
給你的信息:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
[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
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
}
如果還有什么需要,請盡管問! 我真正的問題是我不知道如何找到問題。 我可以使用什么工具?
在此先感謝您的幫助。
我可以確認我的 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,
},
},
}
並且我在瀏覽我的網站時沒有登錄任何錯誤(頁面始終顯示,其他頁面從不顯示,有些有時顯示,有時不顯示)。
我嘗試遵循本教程,即不使用中間 sock 文件。 沒有成功,即使在執行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,點擊這個鏈接: https : //www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18 -04有時我的網站變慢並出現 502 錯誤。 尤其是在閑置的時候又卷土重來。 先點擊get 502,再點擊兩次網站運行順利。 我創立了這個: https ://www.datadoghq.com/blog/nginx-502-bad-gateway-errors-gunicorn/ 在我的情況下:我增加了 gunicorn 和 nginx 超時:
nginx:
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
}
槍炮服務:
ExecStart=/home/myself/django/myproject/venvprod/bin/gunicorn \
--access-logfile - \
--bind unix:/run/gunicorn.sock \
--timeout 60 \
myproject.wsgi:application
經過一千次搜索,我找到了解決問題的方法。
我在虛擬環境中安裝了 gunicorn。 我必須轉到安裝文件夾 /virtual/venv/lib/python3.7/site-packages/gunicorn$ 並修改 config.py 文件,將超時時間增加到 3000。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.