Django: 400 Error with Debug=False and ALLOWED_HOSTS=["*"]
<p>I'm trying to run a relatively simple Django server on python 3.5.3 on an Ubuntu DigitalOcean droplet. I'm using a Gunicorn server with nginx. The server runs fine when DEBUG=True in settings.py. But when I set it to False, I get a 400 error when trying to visit the page. I tried setting ALLOWED_HOSTS to ['*'], but I still get the same error. I've looked on a lot of forums and many questions on SO but none of the solutions have worked.</p>

<p>EDIT:
Gunicorn logs, from startup</p>

<pre><code>[2016-09-13 00:02:01 +0000] [27160] [DEBUG] Current configuration:
  nworkers_changed: &lt;function NumWorkersChanged.nworkers_changed at 0x7f9ac58d3d90&gt;
  worker_class: sync
  pre_fork: &lt;function Prefork.pre_fork at 0x7f9ac58c8f28&gt;
  limit_request_fields: 100
  statsd_host: None
  limit_request_field_size: 8190
  default_proc_name: KivaWebsite.wsgi
  capture_output: False
  raw_env: []
  pidfile: None
  pythonpath: None
  when_ready: &lt;function WhenReady.when_ready at 0x7f9ac58c8d90&gt;
  post_worker_init: &lt;function PostWorkerInit.post_worker_init at 0x7f9ac58d32f0&gt;
  pre_exec: &lt;function PreExec.pre_exec at 0x7f9ac58d37b8&gt;
  ca_certs: None
  syslog_prefix: None
  django_settings: None
  sendfile: None
  group: 0
  limit_request_line: 4094
  on_starting: &lt;function OnStarting.on_starting at 0x7f9ac58c8a60&gt;
  accesslog: None
  statsd_prefix: 
  threads: 1
  max_requests_jitter: 0
  graceful_timeout: 30
  cert_reqs: 0
  proc_name: None
  spew: False
  loglevel: DEBUG
  pre_request: &lt;function PreRequest.pre_request at 0x7f9ac58d3950&gt;
  timeout: 30
  worker_tmp_dir: None
  on_exit: &lt;function OnExit.on_exit at 0x7f9ac58d3f28&gt;
  tmp_upload_dir: None
  max_requests: 0
  keepalive: 2
  preload_app: False
  logger_class: gunicorn.glogging.Logger
  syslog_facility: user
  forwarded_allow_ips: ['127.0.0.1']
  post_request: &lt;function PostRequest.post_request at 0x7f9ac58d3a60&gt;
  certfile: None
  bind: ['unix:/home/thomas/KivaWebsite/KivaWebsite.sock']
  ssl_version: 3
  access_log_format: %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"
  errorlog: logs2.log
  logconfig: None
  umask: 0
  proxy_allow_ips: ['127.0.0.1']
  reload: False
  check_config: False
  workers: 1
  worker_connections: 1000
  syslog_addr: udp://localhost:514
  chdir: /home/thomas/KivaWebsite
  paste: None
  keyfile: None
  on_reload: &lt;function OnReload.on_reload at 0x7f9ac58c8bf8&gt;
  post_fork: &lt;function Postfork.post_fork at 0x7f9ac58d3158&gt;
  worker_int: &lt;function WorkerInt.worker_int at 0x7f9ac58d3488&gt;
  backlog: 2048
  syslog: False
  worker_abort: &lt;function WorkerAbort.worker_abort at 0x7f9ac58d3620&gt;
  worker_exit: &lt;function WorkerExit.worker_exit at 0x7f9ac58d3bf8&gt;
  daemon: False
  user: 0
  proxy_protocol: False
  config: None
  secure_scheme_headers: {'X-FORWARDED-SSL': 'on', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-PROTOCOL': 'ssl'}
  suppress_ragged_eofs: True
  do_handshake_on_connect: False
  ciphers: TLSv1
  enable_stdio_inheritance: False
[2016-09-13 00:02:01 +0000] [27160] [INFO] Starting gunicorn 19.6.0
[2016-09-13 00:02:01 +0000] [27160] [DEBUG] Arbiter booted
[2016-09-13 00:02:01 +0000] [27160] [INFO] Listening at: unix:/home/thomas/KivaWebsite/KivaWebsite.sock (27160)
[2016-09-13 00:02:01 +0000] [27160] [INFO] Using worker: sync
[2016-09-13 00:02:01 +0000] [27163] [INFO] Booting worker with pid: 27163
[2016-09-13 00:02:01 +0000] [27160] [DEBUG] 1 workers
[2016-09-13 00:02:25 +0000] [27163] [DEBUG] GET /
</code></pre>

<p>EDIT: Nginx logs show an error:</p>

<pre><code>request: "GET / HTTP/1.1", upstream: "http://unix:/home/thomas/KivaWebsite/KivaWebsite.sock:/", host: "104.131.153.181"
2016/09/12 12:06:47 [crit] 22081#22081: *96 connect() to unix:/home/thomas/KivaWebsite/KivaWebsite.sock failed (2: No such file or directory)
</code></pre>

<p>However, I have checked and the file definitely exists. This is my nginx config file:</p>

<pre><code>server {
    listen 80;
    server_name 104.131.153.181;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/thomas/KivaWebsite;
    }

    location / {
        include proxy_params;
        proxy_set_header Host $host;
        proxy_pass http://unix:/home/thomas/KivaWebsite/KivaWebsite.sock;
    }
}
</code></pre>

<p>Is there anything wrong with it?</p>

<p>Make sure that the config in nginx has the proper alias using absolute paths (ie: <code>/etc/nginx/sites-enabled</code>) 
It doesn't work if the alias was done with a relative path for whatever reason. </p>

<p>This are the appropriate settings for nginx at the <code>location / {}</code> section:</p>

<pre><code>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
include uwsgi_params;
proxy_pass http://unix:/home/thomas/KivaWebsite/KivaWebsite.sock;
</code></pre>

python django nginx gunicorn digital-ocean