【发布时间】:2015-03-13 13:51:33
【问题描述】:
被困了一段时间试图弄清楚为什么我不断收到 400 错误,nginx 日志给我留下的线索不多(访问日志有效,表明 DNS 是正确的)。
Gunicorn 跑步者正在运行并且可以在本地访问网站(通过“链接 127.0.0.1:8000”),但是在 Nginx 和 Gunicorn 之间似乎出了点问题,因为我无法使用域。
解决这个问题会让我很开心 :)
在 Django 配置中添加
ALLOWED_HOSTS = ['mydomain.tdl', 'www.mydomain.tdl']
Nginx 配置:
#user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /usr/local/etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx-access.log;
error_log /var/log/nginx-error.log info;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
#text/javascript;
##
# Virtual Host Configs
##
upstream myapp_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/django/myapp/run/gunicorn.sock fail_timeout=0;
}
server {
#listen 80 is default
server_name mydomain.se;
return 301 http://www.mydomain.se$request_uri;
}
server {
listen 80;
server_name www.mydomain.se;
# return 301 http://www.mydomain.se$request_uri;
client_max_body_size 4G;
access_log /webapps/django/myapp/logs/nginx-access.log;
error_log /webapps/django/myapp/logs/nginx-error.log info;
location /static/ {
alias /webapps/django/myapp/static/;
}
location /media/ {
alias /webapps/django/myapp/casinoguden/media/;
}
location / {
if (!-f $request_filename) {
proxy_pass http://myapp_app_server;
break;
}
# include includes/botblock;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/django/myapp/static/;
}
}
}
Gunicorn 配置 shell 脚本:
#!/usr/local/bin/bash
NAME="myapp" # Name of the application
DJANGODIR=/webapps/django/myapp/ # Django project directory
PROJECTDIR=/webapps/django/myapp/
SOCKFILE=/webapps/django/myapp/run/gunicorn.sock # we will communicte using this unix socket
USER=david # the user to run as
GROUP=wheel # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=myapp.settings_prod # which settings file should Django use
DJANGO_WSGI_MODULE=myapp.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd /home/david
source venv16/bin/activate
cd $DJANGODIR
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
#export PYTHONPATH=$DJANGODIR:$PYTHONPATH
cd $PROJECTDIR
# 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 gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--bind=127.0.0.1:8000 \
--log-level=debug \
--log-file=-
【问题讨论】:
标签: python django nginx freebsd gunicorn