我有一个金字塔应用程序,在一些地方使用request.environ['REMOTE_ADDR']。
应用程序由端口6543上的Python粘贴提供服务,监听端口80的nginx服务器将请求转发到粘贴服务器。
nginx配置的灵感来源于金字塔食谱:
server {
listen 80; ## listen for ipv4
listen [::]:80 default ipv6only=on; ## listen for ipv6
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;
}在金字塔应用程序中,变量request.environ‘’REMOTE_ADDR‘现在总是等于127.0.0.1。我看到了一些解决这个问题的策略,但我不知道是否有推荐的方法来解决这个问题。
以下是我所考虑的:
if 'HTTP_X_REAL_IP' in event.request.environ: event.request.environ['REMOTE_ADDR'] = event.request.environ['HTTP_X_REAL_IP']
您使用哪种策略来部署金字塔应用程序?如果我有两个nginx代理呢?(第一个服务局域网,第二个服务于直接连接到互联网的机器)。
发布于 2012-02-21 16:24:50
如果您通过paste.deploy.config.PrefixMiddleware在WSGI管道中使用use = egg:PasteDeploy#prefix,它将自动将X-Forwarded-For转换为REMOTE_ADDR。它对反向代理的其他属性也很好,例如,它将将X-Forwarded-Proto转换为wsgi.url_scheme,以确保如果用户使用https进行访问,生成的URL也是https。
http://pythonpaste.org/deploy/class-paste.deploy.config.PrefixMiddleware.html
发布于 2014-01-17 08:56:15
我使用nginx背后的gevent服务器,并使用request.client_addr获取客户端的ip地址。
发布于 2014-12-03 18:10:31
如果您没有WSGI对线,或者不希望使用paste,那么添加一个事件处理程序:
config.add_subscriber(reverseProxyProtocolCorrection,'pyramid.events.NewRequest')事件处理程序可以读到以下内容:
def reverseProxyProtocolCorrection(event):
event.request.scheme = 'https' if event.request.headers['X-Forwarded-Proto']=='https' else 'http'
event.request.remote_addr=parseXForward(event.request.headers['X-Forwarded-For'],event.request.remote_addr)并确保代理设置标头。
https://stackoverflow.com/questions/9380088
复制相似问题