I have a Pyramid application that uses request.environ['REMOTE_ADDR']in some places.
The application is served by Python Paste on port 6543, and the nginx server listening on port 80 sends requests to the Paste server.
The nginx configuration is based on the Pyramid cookbook:
server {
listen 80;
listen [::]:80 default ipv6only=on;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:6543;
}
In the Pyramid application, the variable request.environ ['REMOTE_ADDR'] is now always 127.0.0.1. I see several strategies to solve this problem, but I do not know if there is a recommended way to do this.
Here is what I am considering:
add a NewRequest subscriber who will replace request.environ ['REMOTE_ADDR'] if necessary:
if 'HTTP_X_REAL_IP' in event.request.environ:
event.request.environ['REMOTE_ADDR'] = event.request.environ['HTTP_X_REAL_IP']
use wsgi middleware to modify request.environ before hitting the pyramid layer.
-
Pyramid?
, nginx? ( , - , ).