Django:Debug=False 和 ALLOWED_HOSTS=["*"] 时出现 400 错误

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

我正在尝试 运行 在 Python 3.5.3 上 Ubuntu DigitalOcean droplet 上的一个相对简单的 Django 服务器。我正在使用带有 nginx 的 Gunicorn 服务器。

settings.py 中的 DEBUG=True 时,服务器 运行 正常。但是,当我将其设置为 False 时,我在尝试访问该页面时收到 400 错误。我尝试设置 ALLOWED_HOSTS = ['*'],但我仍然得到同样的错误。

我查看了很多论坛和很多关于 SO 的问题,但 none 的解决方案有效。

编辑

Gunicorn 启动日志:

[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
  statsd_prefix: 
  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: ['127.0.0.1']
  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: ['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: <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 /

编辑

Nginx 日志显示错误:

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)

不过,我已经检查过,该文件确实存在。这是我的 nginx 配置文件:

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;
    }
}

有什么问题吗?

确保 nginx 中的配置具有使用绝对路径的正确别名(即:/etc/nginx/sites-enabled) 如果出于任何原因使用相对路径完成别名,则它不起作用。

这是 location / {} 部分中 nginx 的适当设置:

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;