benoitc/gunicorn — pull request #633 by mardiros (2013-10-18 14:56:30+00:00)
Title: Do not override REMOTE_ADDR with X-Fowarded-For

From rfc 3875
The REMOTE_ADDR variable MUST be set to the network address of the
client sending the request to the server.

For exemple
---
# WSGI app
def application(environ, start_response):
start_response('200 OK', [('Content-Type','text/html')])
ret = []
for k, v in environ.items():
if k not in ('REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR'):
continue
ret.append('{0}={1}\n'.format(k, v))
return ret
---
# Client
import requests
response = requests.get('http://localhost:5000',
headers={'X-Forwarded-For': '1.2.3.4'})

print response.text

---
Running server and client:

python client.py
REMOTE_ADDR=127.0.0.1
HTTP_X_FORWARDED_FOR=1.2.3.4

---

Before that patch, using gunicorn, it is impossible to get the proxy address inside the WSGi application.
Every other tested WSGI Server (mod_wsgi and uwsgi), it works.
Gunicorn should works too.