Skip to content

Commit

Permalink
Add XForwardedForMiddleware middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Justine Tunney committed Aug 20, 2011
1 parent acc8a89 commit 971f7c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions occupywallst/middleware.py
Expand Up @@ -21,3 +21,18 @@ class NeverCache(object):
def process_response(self, request, response):
add_never_cache_headers(response)
return response


class XForwardedForMiddleware(object):
"""Replace ``REMOTE_ADDR`` with ``HTTP_X_FORWARDED_FOR``
When reverse proxying from nginx, we receive a tcp connection from
localhost which isn't the client's real ip address. Normally
reverse proxies are configured to set the ``X-Forwarded-For``
header which gives us the actual client ip.
"""

def process_request(self, request):
if 'HTTP_X_FORWARDED_FOR' in request.META:
request.META['REMOTE_ADDR'] = request.META['HTTP_X_FORWARDED_FOR']
request.META['REMOTE_HOST'] = None
1 change: 1 addition & 0 deletions occupywallst/settings.py
Expand Up @@ -105,6 +105,7 @@
]

MIDDLEWARE_CLASSES = [
'occupywallst.middleware.XForwardedForMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down

0 comments on commit 971f7c7

Please sign in to comment.