我知道在SO上有很多这样的问题,但它们似乎都没有回答我的特定问题。
我知道Django的ALLOWED_HOSTS
值阻止了我的IP上没有80
值的任何端口Host:
的请求,并且当请求时进来没有正确的价值,Django正在给我发电子邮件。我也知道slick Nginx hack会让这个问题消失,但我试图了解一个这样的请求的性质,并确定这是否是一个我需要担心的安全问题。
这样的要求是有道理的:
[Django] ERROR: Invalid HTTP_HOST header: '203.0.113.1'. You may need to add u'203.0.113.1' to ALLOWED_HOSTS.
但是有一种让我感到害怕:
[Django] ERROR: Invalid HTTP_HOST header: u'/run/my_project_name/gunicorn.sock:'.
这是否意味着请求者将Host: /run/my_project_name/gunicorn.sock
发送到服务器?如果是这样,他们如何拥有.sock
文件的路径名?我的服务器是否以某种方式泄露了这些信息?
此外,由于我正在运行Django 1.6.5,我根本不明白为什么我会收到这些电子邮件,因为this ticket已被标记为已修复一段时间了
有人可以解释我错过的内容吗?
这是我的settings.LOGGING
变量:
{
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {'()': 'django.utils.log.RequireDebugFalse'}
},
'formatters': {
'simple': {'format': '%(levelname)s %(message)s'},
'verbose': {'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'verbose',
'level': 'DEBUG'
},
'mail_admins': {
'class': 'django.utils.log.AdminEmailHandler',
'filters': ['require_debug_false'],
'level': 'ERROR'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True
},
'my_project_name': {
'handlers': ['console'],
'level': 'DEBUG'
}
},
'version': 1
}
这是我的nginx配置:
worker_processes 1;
pid /run/nginx.pid;
error_log /var/log/myprojectname/nginx.error.log debug;
events {
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/myprojectname/nginx.access.log combined;
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
upstream app_server {
server unix:/run/myprojectname/gunicorn.sock fail_timeout=0;
}
server {
listen 80 default;
listen [::]:80 default;
client_max_body_size 4G;
server_name myprojectname.mydomain.tld;
keepalive_timeout 5;
root /var/www/myprojectname;
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_pass http://app_server;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /tmp;
}
}
}
最后,我在我的nginx访问日志中找到了这个。它对应于抱怨/run/myprojectname/gunicorn.sock是无效的HTTP_HOST标头的电子邮件。*
当然,这一切都在一条线上:
2014/09/05 20:38:56 [info] 12501#0: *513 epoll_wait() reported that client
prematurely closed connection, so upstream connection is closed too while sending
request to upstream, client: 54.84.192.68, server: myproject.mydomain.tld, request:
"HEAD / HTTP/1.0", upstream: "http://unix:/run/myprojectname/gunicorn.sock:/"
显然,我仍然不知道这意味着什么: - (
settings.LOGGING
答案 0 :(得分:21)
似乎
proxy_set_header Host $http_host
proxy_set_header Host $host
和server_name
should be set appropriately到用于访问服务器的地址。如果您想要全部捕获,请使用server_name www.domainname.com ""
(doc here)。
我不确定,但我认为如果客户端没有发送Host:
标头,您所看到的内容就会发生。由于nginx没有收到Host:
标头,因此没有Host:
标头传递给gunicorn。在这一点上,我认为gunicorn填充Host:
作为套接字路径并告诉Django这是因为使用了连接。使用$host
并在nginx中设置server_name
应确保Host:
正确传递给gunicorn并解决此问题。
对于电子邮件,根据您链接的故障单中的commit,看起来仍然会为不允许的主机发送电子邮件。添加到文档中的还有suggested a way to disable the emails being sent:
'loggers': {
'django.security.DisallowedHost': {
'handlers': ['null'],
'propagate': False,
},
答案 1 :(得分:7)
我发现一些评论表明抑制电子邮件不是一个好主意,因为它没有直接解决问题。我发现最有效的解决方案是将以下内容添加到您的nginx设置中:
server {
...
## Deny illegal Host headers
if ($host !~* ^(mydomain.com|www.mydomain.com)$ ) {
return 444;
}
}
有关详细信息:https://snakeycode.wordpress.com/2015/05/31/django-error-invalid-http_host-header/
博客文章引用了这个问题。
答案 2 :(得分:4)
我知道这是一个老问题,但问题就在今天。 Django docs上推荐的解决方案是在你的nginx配置中添加一个“catch all”nginx服务器:
server {
listen 80 default_server;
return 444;
}
official nginx docs推荐相同的解决方案,给出或采取一些语法细微差别。
这样,请求不会转到django,当nginx收到格式错误的请求时,连接会立即关闭。