1
0
mirror of https://github.com/django/django.git synced 2025-07-05 10:19:20 +00:00

[1.2.X] Fixed #11877 -- Documented that HttpRequest.get_host() fails behind multiple reverse proxies, and added an example middleware solution. Thanks to Tom Evans for the report, and arnav for the patch.

Backport of [14493] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14494 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Gabriel Hurley 2010-11-07 23:03:47 +00:00
parent 0164ee0082
commit 6e201bcae2

View File

@ -200,6 +200,29 @@ Methods
Example: ``"127.0.0.1:8000"`` Example: ``"127.0.0.1:8000"``
.. note:: The :meth:`~HttpRequest.get_host()` method fails when the host is
behind multiple proxies. One solution is to use middleware to rewrite
the proxy headers, as in the following example::
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()
.. method:: HttpRequest.get_full_path() .. method:: HttpRequest.get_full_path()
Returns the ``path``, plus an appended query string, if applicable. Returns the ``path``, plus an appended query string, if applicable.