Django:400错误,Debug = False,ALLOWED_HOSTS = [“*”]

时间:2016-09-12 22:42:24

标签: python django nginx gunicorn digital-ocean

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

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

我看过很多论坛和很多关于SO的问题,但没有一个解决方案有效。

修改

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

它有什么问题吗?

1 个答案:

答案 0 :(得分:2)

确保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;