错误请求(400)Nginx + Gunicorn + Django + FreeBSD

时间:2015-03-13 13:51:34

标签: python django nginx freebsd gunicorn

一直试图弄清楚为什么我继续得到400错误,nginx日志给我留下了不多的线索(访问日志工作表明DNS是正确的)。

Gunicorn跑步者正在运行,网站可以在本地访问(通过“链接127.0.0.1:8000”),但是在Nginx和Gunicorn之间似乎出现了一些问题,因为我无法访问网站域。

解决这个问题会让我很开心:)


在Django config中添加

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=-



1 个答案:

答案 0 :(得分:1)

通过在nginx.conf

中的location指令下添加:proxy_set_header Host $host;来解决