Ever since I deployed a site running Django 1.7 alpha (checked out from Git), I've been occasionally receiving error messages with titles like:
"Invalid HTTP_HOST header: 'xxx.xxx.com'"
I realize that this is due to the Host:
HTTP header being set to a hostname not listed in ALLOWED_HOSTS
. However, I have no control over when and how often someone sends a request to the server with a forged hostname. Therefore I do not need a bunch of error emails letting me know that someone else is attempting to do something fishy.
Is there any way to disable this error message? The logging settings for the project look like this:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
for multiple valid hosts you can:
You can add this to the
loggers
section of your logging configuration:This sets the logging threshold to above the
ERROR
level that Django uses when aSuspiciousOperation
is detected.Alternatively, you can use e.g. a
FileHandler
to log these events without emailing them to you. For example, to use a dedicated file just for these specific events, you could add this to thehandlers
section:and then use this in the
loggers
section:Note that the suggestion made in the Django docs, to use
depends on you running Python 2.7 or later - on 2.6,
logging
doesn't have aNullHandler
.In setting.py set:
you could silence that particular SuspiciousOperation with something like
see this for more reference https://docs.djangoproject.com/en/dev/topics/logging/#django-security
EDIT
you also need to add a 'null' handler:
probably you only need to add this and modify the level of error (replacing DEBUG with 'ERROR').
as always refer to the the documentation for the complete syntax and semantic.
The other answers on this page are correct if you're simply looking to hide or disable the warning. If you're intentionally allowing every hostname the special value of
*
can be used as theALLOWED_HOSTS
setting.To prevent hostname checking entirely, add the following line to your
settings.py
:Source: https://github.com/django/django/blob/master/django/http/request.py#L544-L563
Here's NGINX example that should prevent your django from receiving rubbish requests.