Django:400 错误,Debug=False 和 ALLOWED_HOSTS=["*"]
Posted
技术标签:
【中文标题】Django:400 错误,Debug=False 和 ALLOWED_HOSTS=["*"]【英文标题】:Django: 400 Error with Debug=False and ALLOWED_HOSTS=["*"] 【发布时间】:2017-01-20 10:51:25 【问题描述】:我正在尝试在 Ubuntu DigitalOcean 液滴上的 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;
有什么问题吗?
【问题讨论】:
您是否尝试查看服务器日志? 400 表示可能是格式错误的请求或其他客户端错误 我的(随机)猜测是 nginx 从未期望 SSL 从 wsgi_pass 返回。我从不使用 gunicorn,但最后一行 ([DEBUG] GET /
) 表明请求已到达 gunicorn。您还拥有 /var/log/nginx 中的 nginx 日志
您是否使用绝对路径为 nginx 配置起了别名? (即:/etc/nginx/sites-enabled
)如果别名是使用相对路径完成的(出于某种原因),则它不起作用。尝试在proxy_pass
行之前的location /
上设置此设置,而不是当前行: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;
@Av4t4r 非常感谢!我的配置文件中的更改有效!让它成为一个答案,我会接受它。
@TheRuler 完成 :)。但是,出于安全原因,我强烈建议您将 ALLOWED_HOSTS
更改为仅接受来自 localhost 和当前内部网络的连接(对于 DigitalOcean,我认为它应该只设置为 localhost)
【参考方案1】:
确保 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;
【讨论】:
以上是关于Django:400 错误,Debug=False 和 ALLOWED_HOSTS=["*"]的主要内容,如果未能解决你的问题,请参考以下文章
使用 Apache/NginX 部署时出现 Django Bad Request(400) 错误