Django gunicorn Nginx 设置仅显示 404 页面
Posted
技术标签:
【中文标题】Django gunicorn Nginx 设置仅显示 404 页面【英文标题】:Django gunicorn Nginx setup only showing 404 page 【发布时间】:2016-04-16 06:42:18 【问题描述】:按照本教程,我在 Debian 7 vps 上设置了一个 Django 开发盒
http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/
似乎一切正常,但是当我尝试访问我的域 my.site 时,我从 nginx 收到 404 错误页面。 如果我使用远程桌面,如果我直接启动 gunicorn,我可以使用 localhost 或 127.0.0.1:8000 或 8001 查看 django 项目。 我已经在谷歌和这里搜索了多种解决方案,但似乎没有一个能解决我的问题?
允许的主机设置为带和不带 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"
姜戈
(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 站点可用
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】:您能否在不激活虚拟环境的情况下自行运行 gunicorn bash 脚本?
你能用一个简单的 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;
【讨论】:
是的,我可以运行 gunicorn 脚本,它启动 gunicorn 没问题,我将命令粘贴到 gunicorn 脚本块中。我也可以用主管启动它。问题似乎出在 DEBUG = FALSE ALLOWED HOSTS 上。如果我打开调试,我可以访问该站点,关闭调试并进入我的域它根本不会加载该站点。我尝试输入 localhost 127.0.0.1 我的域和服务器的 ip,没有任何东西可以显示页面。作为测试,我将 * 添加到允许的主机以查看它是否可以访问,但它只显示 500 错误页面。 更改proxy_pass license_app_server;到 proxy_pass 127.0.0.1:8000;并运行 django 开发服务器,检查是否正常以上是关于Django gunicorn Nginx 设置仅显示 404 页面的主要内容,如果未能解决你的问题,请参考以下文章
使用 nginx 和 gunicorn 运行多个 django 项目
使用 Gunicorn 和 nginx 部署 Django 项目
django、gunicorn 和 Nginx 的 502 错误
仅使用 gunicorn、django 和 whitenoise 我如何提供媒体服务?