Add middleware for fix "get_host() method fails when the host is behind multiple proxies"

This commit is contained in:
Andrii Marynets 2017-06-30 08:44:39 +03:00
parent 5b53daa14a
commit 9431e9846c
2 changed files with 20 additions and 1 deletions

View file

@ -118,6 +118,7 @@ INSTALLED_APPS = (
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'utils.middleware.MultipleProxyMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
@ -515,5 +516,6 @@ DCL_SUPPORT_FROM_ADDRESS = env('DCL_SUPPORT_FROM_ADDRESS')
# Settings for Google analytics
GOOGLE_ANALYTICS_PROPERTY_IDS = {
'datacenterlight.ch': 'UA-62285904-9',
'digitalglarus.ch': 'UA-62285904-2'
'digitalglarus.ch': 'UA-62285904-2',
'127.0.0.1:8000': 'test'
}

17
utils/middleware.py Normal file
View file

@ -0,0 +1,17 @@
class MultipleProxyMiddleware(object):
FORWARDED_FOR_FIELDS = [
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED_HOST',
'HTTP_X_FORWARDED_SERVER',
]
def process_request(self, request):
"""
Rewrites the proxy headers so that only the most
recent proxy is used.
"""
for field in self.FORWARDED_FOR_FIELDS:
if field in request.META:
if ',' in request.META[field]:
parts = request.META[field].split(',')
request.META[field] = parts[-1].strip()