Django gunicorn Nginx设置仅显示404页面

时间:2016-01-11 20:17:30

标签: django nginx debian gunicorn supervisor

按照本教程

,我在Debian 7 vps上设置了一个Django开发框

http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/

一切似乎都正常工作但是当我尝试访问我的域my.site时,我从nginx获得了404错误页面。 如果我使用远程桌面,我可以使用localhost或127.0.0.1:8000或8001看到django项目,如果我直接启动gunicorn。 我已经搜索谷歌和这里的多种解决方案,但似乎没有摆脱我的问题?

允许的主机设置为包含和不包含www。

的域

什么会导致此错误? 我只是希望能够在浏览器中浏览my.site并查看django项目,该项目似乎无法正常工作

有人可以让我了解可能发生的事情!? 这是我的配置

的nginx-error.log中

016/01/11 03:17:51 [error] 29962#0: *5 open() "/webapps/license/static/500.html" failed (2: No such file or directory), client: x.x.x.x, server: my.site, request: "GET / HTTP/1.1", upstream: "http://unix:/webapps/license/run/gunicorn.sock:/", host: "www.my.site"

Django的

(license)app_usr@dev2:~/licdb$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
January 11, 2016 - 19:47:07
Django version 1.9.1, using settings 'licdb.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Gunicorn开始剧本

#!/bin/bash

NAME="licdb"                                  # Name of the application
DJANGODIR=/webapps/license/licdb             # Django project directory
SOCKFILE=/webapps/license/licdb/run/gunicorn.sock  # we will communicte using this unix socket
USER=app_usr                                        # the user to run as
GROUP=webapps                                     # the group to run as
NUM_WORKERS=3                                     # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=licdb.settings             # which settings file should Django use
DJANGO_WSGI_MODULE=licdb.wsgi                     # WSGI module name

echo "Starting $NAME as `whoami`"

# Activate the virtual environment

cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --bind=unix:$SOCKFILE \
  --log-level=debug \
  --log-file=-




(license)root@dev2:/webapps/license/bin# ./gunicorn_start
Starting licdb as root
[2016-01-11 03:58:17 +0000] [30278] [DEBUG] Current configuration:
  proxy_protocol: False
  worker_connections: 1000
  statsd_host: None
  max_requests_jitter: 0
  post_fork: <function post_fork at 0x1060de8>
  pythonpath: None
  enable_stdio_inheritance: False
  worker_class: sync
  ssl_version: 3
  suppress_ragged_eofs: True
  syslog: False
  syslog_facility: user
  when_ready: <function when_ready at 0x1060b18>
  pre_fork: <function pre_fork at 0x1060c80>
  cert_reqs: 0
  preload_app: False
  keepalive: 2
  accesslog: None
  group: 999
  graceful_timeout: 30
  do_handshake_on_connect: False
  spew: False
  workers: 3
  proc_name: licdb
  sendfile: None
  pidfile: None
  umask: 0
  on_reload: <function on_reload at 0x10609b0>
  pre_exec: <function pre_exec at 0x1064410>
  worker_tmp_dir: None
  post_worker_init: <function post_worker_init at 0x1060f50>
  limit_request_fields: 100
  on_exit: <function on_exit at 0x1064aa0>
  config: None
  secure_scheme_headers: {'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-SSL': 'on'}
  proxy_allow_ips: ['127.0.0.1']
  pre_request: <function pre_request at 0x1064578>
  post_request: <function post_request at 0x1064668>
  user: 999
  forwarded_allow_ips: ['127.0.0.1']
  worker_int: <function worker_int at 0x1064140>
  threads: 1
  max_requests: 0
  limit_request_line: 4094
  access_log_format: %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"
  certfile: None
  worker_exit: <function worker_exit at 0x10647d0>
  chdir: /webapps/license/licdb
  paste: None
  default_proc_name: licdb.wsgi:application
  errorlog: -
  loglevel: debug
  logconfig: None
  syslog_addr: udp://localhost:514
  syslog_prefix: None
  daemon: False
  ciphers: TLSv1
  on_starting: <function on_starting at 0x1060848>
  worker_abort: <function worker_abort at 0x10642a8>
  bind: ['unix:/webapps/license/licdb/run/gunicorn.sock']
  raw_env: []
  reload: False
  check_config: False
  limit_request_field_size: 8190
  nworkers_changed: <function nworkers_changed at 0x1064938>
  timeout: 30
  ca_certs: None
  django_settings: None
  tmp_upload_dir: None
  keyfile: None
  backlog: 2048
  logger_class: gunicorn.glogging.Logger
  statsd_prefix:
[2016-01-11 03:58:17 +0000] [30278] [INFO] Starting gunicorn 19.4.5
[2016-01-11 03:58:17 +0000] [30278] [DEBUG] Arbiter booted
[2016-01-11 03:58:17 +0000] [30278] [INFO] Listening at: unix:/webapps/license/licdb/run/gunicorn.sock (30278)
[2016-01-11 03:58:17 +0000] [30278] [INFO] Using worker: sync
[2016-01-11 03:58:17 +0000] [30289] [INFO] Booting worker with pid: 30289
[2016-01-11 03:58:17 +0000] [30294] [INFO] Booting worker with pid: 30294
[2016-01-11 03:58:17 +0000] [30295] [INFO] Booting worker with pid: 30295
[2016-01-11 03:58:17 +0000] [30278] [DEBUG] 3 workers

nginx sites-available

upstream license_app_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).


  server unix:/webapps/license/run/gunicorn.sock fail_timeout=0;

}


server {

    listen   80;
    server_name my.site;
    client_max_body_size 4G;
    access_log /webapps/license/logs/nginx-access.log;
    error_log /webapps/license/logs/nginx-error.log;

    location /static/ {
        alias   /webapps/license/static/;
    }


    location /media/ {
        alias   /webapps/license/media/;

    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        # proxy_buffering off;
        # Try to serve static files from nginx, no point in making an
        # *application* server like Unicorn/Rainbows! serve static files.

        if (!-f $request_filename) {
            proxy_pass http://license_app_server;

            break;

        }

    }


    # Error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /webapps/license/static/;

    }

}

(license)root@dev2:/etc/nginx# supervisorctl start license
license: started
(license)root@dev2:/etc/nginx# supervisorctl status license
license                          RUNNING    pid 30223, uptime 0:00:19
(license)root@dev2:/etc/nginx#

supervisor.conf

[program:license]
command = /webapps/license/bin/gunicorn_start                    ; Command to start app
user = app_usr                                                          ; User to run as
stdout_logfile = /webapps/license/logs/gunicorn_supervisor.log   ; Where to write log messages
redirect_stderr = true                                                ; Save stderr in the same log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8nvironment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8a

目录结构

|-- license
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- admin.py
|   |-- admin.pyc
|   |-- apps.py
|   |-- migrations
|   |   |-- __init__.py
|   |   `-- __init__.pyc
|   |-- models.py
|   |-- models.pyc
|   |-- tests.py
|   `-- views.py
|-- db.sqlite3
|-- licdb
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.pyc
|   |-- urls.py
|   |-- urls.pyc
|   |-- wsgi.py
|   `-- wsgi.pyc
|-- licdb.tx
|-- manage.py
`-- run
    `-- gunicorn.sock

1 个答案:

答案 0 :(得分:1)

  1. 您是否能够在没有激活虚拟环境的情况下自行运行gunicorn bash脚本?

  2. 您可以使用简单的nginx配置进行检查,如:

    server {
    
       listen 80;
    
       server_name SITENAME www.SITENAME;
    
    
       location /static {
    
        alias /path/to/your/staticfiles;
    
       }
    
    
        location / {
    
        proxy_set_header Host $host;
    
        proxy_pass http://unix:/path/to/your/gunicorn/socket;
    
       }
    }