![](/img/trans.png)
[英]Getting 502 Bad Gateway error and sending a email with django, nginx, gunicorn
[英]Bad Gateway 502 Error with Django, Gunicorn and Nginx
我正在嘗試使用Gunicorn和Nginx在Django上運行項目。 在DigitalOcean OneClick安裝映像上,我的項目在沒有virtualenv和全局Django安裝的情況下都能正常運行。 但是,當我為不同的Django版本創建虛擬環境時,我無法使其正常工作。 因此,請有人為我提供一些使用虛擬環境在Ubuntu上托管多站點的幫助。 我的Gunicorn虛擬環境設置如下:
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectadly trigger a respawn
respawn
setuid django
setgid django
chdir /home/django
exec gunicorn \
--name=myproject2\
--pythonpath=myproject2\
--bind=127.0.0.1:9500 \
--config /etc/gunicorn.d/gunicorn.py \
myproject2.wsgi:application
我的第二個項目的Nginx設置是:
upstream ashyanaa_server {
server 127.0.0.1:9500 fail_timeout=0;
}
server {
listen 80;
listen [::]:80;
root /home/django/myproject2;
index index.html index.htm;
client_max_body_size 4G;
server_name www.myproject2.com;
keepalive_timeout 5;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2|woff|ttf)$ {
expires 365d;
}
# Your Django project's media files - amend as required
location /media {
alias /home/django/myproject2/media/;
}
# your Django project's static files - amend as required
location static/static-only {
alias /home/django/myproject2/static-only/;
}
# Django static images
location /static/myproject2/images {
alias /home/django/myproject2/static-only/images/;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://myproject2_server;
}
我的第一個項目設置與第二個項目設置唯一不同的是,我為第二個項目使用虛擬環境,並且顯然我必須為新項目使用不同的端口。
“錯誤的網關”表示Nginx無法連接Gunicorn進程。
curl http://127.0.0.1:9500/
會發生什么? 您收到Gunicorn的回應了嗎? 這是由於缺乏對Nginx的了解。 我在Nginx中添加了www.mydomain.com,但是我習慣在瀏覽器中輸入沒有www的域名。 我只是添加了“ mydomain.com”和“ www.mydomain.com”。 因此,現在兩者都可以正常工作。 如果所有設置正確,但仍然得到502,供其他用戶使用,這意味着您要查找的地址未在Nginx中列出。 這可能是原因之一。 謝謝你們的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.