TheRuler TheRuler - 1 month ago 9x
Python Question

Django: 400 Error with Debug=False and ALLOWED_HOSTS=["*"]

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

Gunicorn logs, from startup

[2016-09-13 00:02:01 +0000] [27160] [DEBUG] Current configuration:
nworkers_changed: <function NumWorkersChanged.nworkers_changed at 0x7f9ac58d3d90>
worker_class: sync
pre_fork: <function Prefork.pre_fork at 0x7f9ac58c8f28>
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: <function WhenReady.when_ready at 0x7f9ac58c8d90>
post_worker_init: <function PostWorkerInit.post_worker_init at 0x7f9ac58d32f0>
pre_exec: <function PreExec.pre_exec at 0x7f9ac58d37b8>
ca_certs: None
syslog_prefix: None
django_settings: None
sendfile: None
group: 0
limit_request_line: 4094
on_starting: <function OnStarting.on_starting at 0x7f9ac58c8a60>
accesslog: None
threads: 1
max_requests_jitter: 0
graceful_timeout: 30
cert_reqs: 0
proc_name: None
spew: False
loglevel: DEBUG
pre_request: <function PreRequest.pre_request at 0x7f9ac58d3950>
timeout: 30
worker_tmp_dir: None
on_exit: <function OnExit.on_exit at 0x7f9ac58d3f28>
tmp_upload_dir: None
max_requests: 0
keepalive: 2
preload_app: False
logger_class: gunicorn.glogging.Logger
syslog_facility: user
forwarded_allow_ips: ['']
post_request: <function PostRequest.post_request at 0x7f9ac58d3a60>
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: ['']
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: <function OnReload.on_reload at 0x7f9ac58c8bf8>
post_fork: <function Postfork.post_fork at 0x7f9ac58d3158>
worker_int: <function WorkerInt.worker_int at 0x7f9ac58d3488>
backlog: 2048
syslog: False
worker_abort: <function WorkerAbort.worker_abort at 0x7f9ac58d3620>
worker_exit: <function WorkerExit.worker_exit at 0x7f9ac58d3bf8>
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 /

EDIT: Nginx logs show an error:

request: "GET / HTTP/1.1", upstream: "http://unix:/home/thomas/KivaWebsite/KivaWebsite.sock:/", host: ""
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)

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

server {
listen 80;

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;

Is there anything wrong with it?


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

This are the appropriate settings for nginx at the location / {} section:

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;