[英]Bad Request (400) Nginx + Gunicorn + Django + FreeBSD
been stuck for a while trying to figure out why I keep on getting a 400 error, nginx log leaves me with not many clues (access log works which indicates DNS is correct). 被困了一段时间试图弄清楚为什么我继续出现400错误,nginx日志给我留下了很多线索(访问日志有效,表明DNS是正确的)。
The Gunicorn runners are running and website can be accessed locally (through "links 127.0.0.1:8000"), however between Nginx and Gunicorn something seems to go wrong since I cannot access the website using the domain. Gunicorn运行程序正在运行,并且可以在本地访问网站(通过“链接127.0.0.1:8000”),但是在Nginx和Gunicorn之间似乎出了问题,因为我无法使用域访问网站。
Solving this would make me very happy :) 解决这个问题会让我非常高兴:)
Added in Django config 在Django配置中添加
ALLOWED_HOSTS = ['mydomain.tdl', 'www.mydomain.tdl']
Nginx config: 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 config shell script: Gunicorn配置外壳脚本:
#!/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=-
Solved by adding: proxy_set_header Host $host;
通过添加以下内容解决: proxy_set_header Host $host;
under the location directive in nginx.conf 在nginx.conf中的location指令下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.