Django 1.8升级后的Nginx 502 Bad Gateway

时间:2016-02-24 11:00:26

标签: python django nginx uwsgi gunicorn

我正在尝试将Django项目的升级从1.4升级到1.8。我的网站在本地运行没有任何问题。但是,当我部署升级的代码时,我得到一个502坏网关。该站点使用Django 1.4在服务器上运行,没有任何问题。

1.4版本的代码使用了Gunicorn和Supervisor。我无法使用Gunicorn运行升级的代码,因此我尝试使用uWsgi,这导致我遇到了同样的问题。

我在同一台服务器上使用Django 1.4的其他网站工作正常。

我无法在任何日志中看到任何有用的错误,我看到的唯一错误是在Nginx日志中:

2016/02/24 10:32:16 [error] 15475#0: *52 upstream prematurely closed connection while reading response header from upstream, client: [IP address], server: myapp.*, request: "GET /api/get_campaign/ HTTP/1.1", upstream: "http://unix:///var/run/supervised-sockets/myapp-uwsgi.sock:/api/get_campaign/", host: "[site-name]"

Nginx配置:

upstream django {
        server unix:///var/run/supervised-sockets/myapp-uwsgi.sock;
    }

server {
    listen 80;

    server_name myapp.*;

    access_log /var/log/nginx/myapp_access.log;
    error_log /var/log/nginx/myapp_error.log notice;

   # rewrite URLs of the form  static/.*/*.gz to non .gz then gzip_static module will check
    # for the compressed version on disk. Round about method to get around django-compressor
    # adding the .gz extension
    rewrite /static/(.*)\.(css|js)\.gz$ /static/$1.$2 last;
    rewrite /media/(.*)\.(css|js)\.gz$ /media/$1.$2 last;

    location /static/CACHE/ {

        root /srv/myapp/releases/current/myapp/myapp/assets/;

        expires max;
        break;
    }



    location /favicon.ico {
        root /srv/myapp/releases/current/myapp/myapp/assets/static/img/;
    }

    location /static/ {

        root /srv/myapp/releases/current/myapp/myapp/assets/;
    }

    location /media/ {
        root /srv/myapp/shared/;
    }

    location / {
       uwsgi_pass django;
       include /srv/myapp/releases/current/myapp/uwsgi_params;
       proxy_set_header HOST $http_host;
       #proxy_pass http://unix:/var/run/supervised-sockets/myapp-uwsgi.sock;
       proxy_pass http://myapp;
       proxy_buffering off;  # no need as services are local
       proxy_connect_timeout 3000s;
       proxy_read_timeout 3000s;
    }

这是我的uWsgi ini文件:

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /srv/myapp/releases/current/myapp
# Django's wsgi file
module          = myapp_wsgi:application
# the virtualenv (full path)
home            = /srv/myapp/
#virtualenv = myapp
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 2
# the socket (use the full path to be safe
socket          = /var/run/supervised-sockets/myapp-uwsgi.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
chown-socket = myapp
# clear environment on exit
vacuum          = true
buffer-size=32768
#die-on-term = true
#wsgi-file = myapp/myapp_wsgi.py

当我运行uWsgi时,这是输出:

[uWSGI] getting INI configuration from myapp_uwsgi.ini
*** Starting uWSGI 2.0.12 (64bit) on [Wed Feb 24 10:45:16 2016] ***
compiled with version: 4.6.3 on 22 February 2016 17:10:22
os: Linux-3.2.0-77-virtual #114-Ubuntu SMP Tue Mar 10 17:38:02 UTC 2015
nodename: ip-10-0-9-42
machine: x86_64
clock source: unix
detected number of CPU cores: 2
current working directory: /srv/myapp/releases/1d4cad9dae00479763636fab34ab2224a59449c4/myapp
detected binary path: /srv/myapp/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /srv/myapp/releases/current/myapp
your processes number limit is 59476
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /var/run/supervised-sockets/myapp-uwsgi.sock fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 2.7.3 (default, Dec 18 2014, 19:25:50)  [GCC 4.6.3]
Set PythonHome to /srv/myapp/
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xf5b420
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 304320 bytes (297 KB) for 2 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0xf5b420 pid: 30534 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 30534)
spawned uWSGI worker 1 (pid: 30541, cores: 1)
spawned uWSGI worker 2 (pid: 30542, cores: 1)

这是Supervisor使用的Gunicorn配置:

[program:myapp-gunicorn]
command=/srv/myapp/bin/gunicorn -b unix:/var/run/supervised-sockets/myapp-gunicorn.sock -w 2 myapp_wsgi:application -t 30 --max-requests 600 --timeout 300
numprocs=1
numprocs_start=0
priority=896
autostart=true
autorestart=true
startsecs=10
startretries=3
exitcodes=0,2
stopsignal=TERM
stopwaitsecs=60
directory=/srv/myapp/releases/current/myapp
user=myapp
redirect_stderr=false
stdout_logfile=/var/log/supervisor/myapp/myapp-gunicorn.out
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=5
stderr_logfile=/var/log/supervisor/myapp/myapp-gunicorn.err
stderr_logfile_maxbytes=10MB
stderr_logfile_backups=5

这是我的Django wsgi文件:

import os
import sys

THIS_DIR = os.path.dirname(os.path.realpath(__file__))

sys.path.insert(0, os.path.join(THIS_DIR, "/"))
sys.path.insert(0, os.path.join(THIS_DIR, "myapp/apps"))
sys.path.insert(0, os.path.join(THIS_DIR, "myapp/lib"))

os.environ["DJANGO_SETTINGS_MODULE"] = "myapp.settings"

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

任何帮助都非常感谢,因为我已经研究了一段时间了。

谢谢

0 个答案:

没有答案