title: Bad Request (400) Nginx + Gunicorn + Django + FreeBSD
body: <p>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).</p>
  
  <p><strong><em>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.</em></strong></p>
  
  <p>Solving this would make me very happy :)
  <br /><br /><br />
  <strong>Added in Django config</strong></p>
  
  <pre><code>ALLOWED_HOSTS = ['mydomain.tdl', 'www.mydomain.tdl']
  </code></pre>
  
  <p><br /></p>
  
  <p><strong>Nginx config:</strong></p>
  
  <pre><code>#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/;
              }
  
          }                                                                                 
  }
  </code></pre>
  
  <p><br /><br />
  <strong>Gunicorn config shell script:</strong></p>
  
  <pre><code>#!/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=-
  </code></pre>
  
  <p><br />
  <br /></p>