与Django,Gunicorn和Nginx的错误网关502错误

时间:2022-10-06 20:28:44

I am trying to run project on Django with Gunicorn and Nginx. On DigitalOcean OneClick install image my project works fine with no virtualenv and with global Django installation. But when I created virtual environment for different Django version I couldn't get it to work. So kindly someone please provide me some help with the multi site hosting on Ubuntu using virtual environment. Follwing is my Gunicorn settings for virtual environment:

我正试图用Gunicorn和Nginx在Django上运行项目。在DigitalOcean OneClick安装映像上,我的项目工作正常,没有virtualenv和全局Django安装。但是当我为不同的Django版本创建虚拟环境时,我无法让它工作。亲切的请有人帮助我使用虚拟环境在Ubuntu上进行多站点托管。 Follwing是我的虚拟环境的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

My Nginx settings for the second project are:

我的第二个项目的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;


    }

Only thing different in my first project settings from the second are that I am using virtual environment for the second project and obviously I had to use different port for new project.

只有我在第一个项目设置中与第二个项目不同的是我正在为第二个项目使用虚拟环境,显然我不得不为新项目使用不同的端口。

2 个解决方案

#1


0  

'Bad Gateway' indicates that Nginx is having trouble connecting the the Gunicorn process.

'Bad Gateway'表示Nginx在连接Gunicorn进程时遇到问题。

  1. Double check that the service that starts Gunicorn (the one defined by the upstart script you posted) is actually running
  2. 仔细检查启动Gunicorn的服务(由您发布的upstart脚本定义的服务)实际上正在运行

  3. What happens when you do curl http://127.0.0.1:9500/? Do you get a response from Gunicorn?
  4. 当你卷曲http://127.0.0.1:9500/时会发生什么?你收到了Gunicorn的回复吗?

#2


0  

This is due to lack of understanding about Nginx. I added www.mydomain.com in Nginx but I have habit of typing domain name without www in browser. I simply added "mydomain.com" and "www.mydomain.com". So now both working without error. For others to follow if you have all the settings correct and still getting 502 that means the address you are looking for is not listed in Nginx. It could be one of the reason. Thanks for help though guys.

这是由于对Nginx缺乏了解。我在Nginx中添加了www.mydomain.com,但我习惯在浏览器中输入没有www的域名。我只是添加了“mydomain.com”和“www.mydomain.com”。所以现在两个都没有错误。如果你有正确的所有设置,仍然得到502,这意味着你正在寻找的地址没有在Nginx中列出。这可能是其中一个原因。感谢大家的帮助。

#1


0  

'Bad Gateway' indicates that Nginx is having trouble connecting the the Gunicorn process.

'Bad Gateway'表示Nginx在连接Gunicorn进程时遇到问题。

  1. Double check that the service that starts Gunicorn (the one defined by the upstart script you posted) is actually running
  2. 仔细检查启动Gunicorn的服务(由您发布的upstart脚本定义的服务)实际上正在运行

  3. What happens when you do curl http://127.0.0.1:9500/? Do you get a response from Gunicorn?
  4. 当你卷曲http://127.0.0.1:9500/时会发生什么?你收到了Gunicorn的回复吗?

#2


0  

This is due to lack of understanding about Nginx. I added www.mydomain.com in Nginx but I have habit of typing domain name without www in browser. I simply added "mydomain.com" and "www.mydomain.com". So now both working without error. For others to follow if you have all the settings correct and still getting 502 that means the address you are looking for is not listed in Nginx. It could be one of the reason. Thanks for help though guys.

这是由于对Nginx缺乏了解。我在Nginx中添加了www.mydomain.com,但我习惯在浏览器中输入没有www的域名。我只是添加了“mydomain.com”和“www.mydomain.com”。所以现在两个都没有错误。如果你有正确的所有设置,仍然得到502,这意味着你正在寻找的地址没有在Nginx中列出。这可能是其中一个原因。感谢大家的帮助。