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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14493 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Gabriel Hurley 2010-11-07 23:02:24 +00:00
parent f90b0f6d69
commit d6e33795a9
1 changed files with 30 additions and 7 deletions

View File

@ -221,6 +221,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.