gpt4 book ai didi

Django 的 SuspiciousOperation 无效的 HTTP_HOST header

转载 作者:行者123 更新时间:2023-11-28 19:32:49 31 4
gpt4 key购买 nike

升级到 Django 1.5 后,我开始收到如下错误:

Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 92, in get_response
response = middleware_method(request)

File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py", line 57, in process_request
host = request.get_host()

File "/usr/local/lib/python2.7/dist-packages/django/http/request.py", line 72, in get_host
"Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): %s" % host)

SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): www.google.com

<WSGIRequest
path:/,
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{},
META:{'CONTENT_LENGTH': '',
'CONTENT_TYPE': '',
'DOCUMENT_ROOT': '/etc/nginx/html',
'HTTP_ACCEPT': 'text/html',
'HTTP_HOST': 'www.google.com',
'HTTP_PROXY_CONNECTION': 'close',
'HTTP_USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'PATH_INFO': u'/',
'QUERY_STRING': '',
'REMOTE_ADDR': '210.245.91.104',
'REMOTE_PORT': '49347',
'REQUEST_METHOD': 'GET',
'REQUEST_URI': '/',
u'SCRIPT_NAME': u'',
'SERVER_NAME': 'www.derekkwok.net',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.0',
'uwsgi.node': 'derekkwok',
'uwsgi.version': '1.4.4',
'wsgi.errors': <open file 'wsgi_errors', mode 'w' at 0xb6d99c28>,
'wsgi.file_wrapper': <built-in function uwsgi_sendfile>,
'wsgi.input': <uwsgi._Input object at 0x953e698>,
'wsgi.multiprocess': True,
'wsgi.multithread': False,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)}>

我在我的 settings.py 文件中设置了 ALLOWED_HOSTS = ['.derekkwok.net']

这是怎么回事?有人冒充谷歌访问我的网站?还是有人错误地设置了他们的 HTTP_HOST header 的良性情况?

最佳答案

如果您使用 Nginx 将请求转发到运行在 Gunicorn/Apache/uWSGI 上的 Django,您可以使用以下方法来阻止不良请求。感谢 @PaulM 的建议。

upstream app_server {
server unix:/tmp/gunicorn_mydomain.example.sock fail_timeout=0;
}

server {

...

## Deny illegal Host headers
if ($host !~* ^(mydomain.example|www.mydomain.example)$ ) {
return 444;
}

location / {
proxy_pass http://app_server;
...
}

}

关于Django 的 SuspiciousOperation 无效的 HTTP_HOST header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15238506/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com