Our app supports arbitrary domains, which means ALLOWED_HOSTS = ['*']
but we handle them manually with a custom middleware:
class AllowedHostMiddleware(object):
def process_request(self, request):
if not domain_allowed(request.get_host()):
raise DisallowedDomain('Invalid domain: {}.format(domain')
Problems:
-
a giant log file is being created because it’s logging every random host on the web, basically crawlers
-
custom error handling middleware doesn’t work because it works only with exceptions thrown by views, not by other middleware.
My understanding is that the way ALLOWED_HOSTS
work natively is that is just eats disallowed requests and doesn’t log them.
-
How can I do the same with my own middleware? Or how can I handle
DisallowedDomain
exceptions and route them to a different log? -
The middleware should block the request but it shouldn’t treat it as an error otherwise it’s going to get logged, pollute the log files and contribute to their massive size.
I’m on Django 1.8.