tornado 反向代理后 获取真实客户端IP
首先,nginx必定会设置一个Header传送过来真实的IP
nginx.conf
server {
proxy_set_header X-Real-IP $remote_addr;
location / {
proxy_pass http://192.168.7.206:8888/;
}
}
然后,tornado的Handler里面进行如下处理:
class Handler(BaseRequestHandler):
def post(self):
self.get()
def get(self):
remote_ip = self.request.headers.get("X-Real-Ip", "")
此处,下面两行其实是没有任何区别的,ng的header到了tornado会做一次统一整理(具体可以参考class HTTPHeaders)。
ip = self.request.headers.get("X-Real-Ip", "")
ip = self.request.headers.get("X-Real-IP", "")
然后,就可以了。
但是,这里为啥还有但是呢?这段日志还有奇怪的地方,它记录的还不对,这个是tornado的默认日志。
[I 150806 14:54:21 web:1811] 200 POST / (192.168.7.62) 2.74ms
看tornado==v4.2.0的代码web.py文件某处一定有如下类似的部分:
def log_request(self, handler):
"""Writes a completed HTTP request to the logs. By default writes to the python root logger. To change
this behavior either subclass Application and override this method,
or pass a function in the application settings dictionary as
``log_function``.
"""
if "log_function" in self.settings:
self.settings["log_function"](handler)
return
if handler.get_status() < 400:
log_method = access_log.info
elif handler.get_status() < 500:
log_method = access_log.warning
else:
log_method = access_log.error
request_time = 1000.0 * handler.request.request_time()
log_method("%d %s %.2fms", handler.get_status(),
handler._request_summary(), request_time)
这块其实就是日志的实现,_request_summary 又是啥?
def _request_summary(self):
return "%s %s (%s)" % (self.request.method, self.request.uri,
self.request.remote_ip)
后续继续看,发现,self.request.remote_ip这个其实就是tornado针对客户端IP做的快捷访问,这里为啥不对呢?
原来要在初始化listen的时候设置一个参数:
def main():
app = Application()
app.listen(options.port, xheaders = True)
产生的效果就是
[I 150806 13:52:53 web:1908] 200 POST / (192.168.7.214) 1.86ms
真实IP反映到Tornado的基础日志里了。
具体实现追踪可以查看 tornado\httpserver.py 中的相关代码。
class _ServerRequestAdapter(httputil.HTTPMessageDelegate):
def headers_received(self, start_line, headers):
if self.server.xheaders:
self.connection.context._apply_xheaders(headers)
class _HTTPRequestContext(object):
def _apply_xheaders(self, headers):
"""Rewrite the ``remote_ip`` and ``protocol`` fields."""
# Squid uses X-Forwarded-For, others use X-Real-Ip
ip = headers.get("X-Forwarded-For", self.remote_ip)
ip = ip.split(',')[-1].strip()
ip = headers.get("X-Real-Ip", ip)
if netutil.is_valid_ip(ip):
self.remote_ip = ip
# AWS uses X-Forwarded-Proto
proto_header = headers.get(
"X-Scheme", headers.get("X-Forwarded-Proto",
self.protocol))
if proto_header in ("http", "https"):
self.protocol = proto_header
def _unapply_xheaders(self):
"""Undo changes from `_apply_xheaders`.
Xheaders are per-request so they should not leak to the next
request on the same connection.
"""
self.remote_ip = self._orig_remote_ip
self.protocol = self._orig_protocol
tornado 反向代理后 获取真实客户端IP的更多相关文章
- nginx设置反向代理,获取真实客户端ip
upstream这个模块提供一个简单方法来实现在轮询和客户端IP之间的后端服务器负荷平衡. upstream abc.com { server 127.0.0.1:8080; server 127.0 ...
- nginx经过多层代理后获取真实来源ip
nginx取 $remote_addr 当做真实ip,而事实上,$http_X_Forwarded_For 才是用户真实ip,$remote_addr只是代理上一层的地址 解决方案: 在 http 模 ...
- JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)
1. JAVA获取客户端请求的当前网络ip地址: /** * 获取客户端请求的当前网络ip * @param request * @return */ public static String get ...
- NGINX前端代理TOMCAT取真实客户端IP
nginx前端代理tomcat取真实客户端IP 使用Nginx作为反向代理时,Tomcat的日志记录的客户端IP就不在是真实的客户端IP,而是Nginx代理的IP.要解决这个问题可以在Nginx配置一 ...
- nginx+tomcat集群配置(3)---获取真实客户端IP
前言: 在初步构建的nginx+tomcat服务集群时, 发现webserver获取到的客户端ip都是同一个, 皆为作为反向代理服务的nginx所在的机器IP. 这不太符合我们的基本需求, 为将来的数 ...
- ABP vNext 审计日志获取真实客户端IP
背景 在使用ABP vNext时,当需要记录审计日志时,我们按照https://docs.abp.io/zh-Hans/abp/latest/Audit-Logging配置即可开箱即用,然而在实际生产 ...
- 关于nginx反向代理后获取不到客户端的真实ip地址问题
前段时间在我的网站上用nginx做了一下反向代理,最近发现不能获取客户端ip了,都是拿到的127.0.0.1的本地ip... 通过查资料后,再去看了看我的配置文件,结果发现我没有如下配置: nginx ...
- nginx做反向负载均衡,后端服务器获取真实客户端ip(转)
首先,在前端nginx上需要做如下配置: location / proxy_set_hearder host $host; proxy_set_header X-forw ...
- nginx反向代理如何获取真实IP?
由于客户端和web服务器之间增加了中间层,因此web服务器无法直接拿到客户端的ip,通过$remote_addr变量拿到的将是反向代理服务器的ip地址. 1.安装--with-http_realip_ ...
随机推荐
- 如何使用花生壳 发布WCF服务 进行外网访问
当我们发布WCF服务的时候,可以直接通过服务器的域名或者IP进行. 但是如果仅仅是通过花生壳进行域名解析,需要我们自己在设置的时候注意以下几点, 直接用图说明问题 1.首先配置花生壳,在红色处填写一个 ...
- 观 GT Java语言管理系统的感悟
继上次java系统考核完... 坦白说,我对我自己写的例子还是很满意的,虽说学长们给的评价不高 ,但我一直以为是学长们对我们的要求太高,以他们的眼光在看待我们,所以我对学长们给的评价并没有太过在意,当 ...
- iOS与JS交互实战篇(ObjC版)
前言 ObjectiveC与Js交互是常见的需求,可对于新手或者所谓的高手而言,其实并不是那么简单明了.这里只介绍iOS7.0后出来的JavaScriptCore framework. 关于JavaS ...
- MR跑百分27不动引发的问题
今天跑MR跑到百分27就卡住不懂,查看JOB history也没看到MR,日志也没看到异常.50030端口页面不知道为什么打不开.由于MR里面设计Hbase就去查了下hbase的表.发现hbase l ...
- RMQ算法模板
分别写了下标从0和1开始的两种 #include<stdio.h> #include<string.h> #include<algorithm> #include& ...
- FPGA书籍
Xilinx FPGA开发实用教程(第2版) 徐文波,田耘 著
- 另类分析SIGSEGV信号
关于SIGSEGV信号的含义就不解释了.网络上有很多解释. 今天记录一下,自己遇到的一个问题,想了好几天都没想出来的.今天终于想到原因了. 过程描述: 有个类 CBase,里面放了一个成员变量 DAT ...
- 解决Spine骨骼混合动画错乱问题
Spine是一个很好的制作2D骨骼动画的软件,其中提供的混合(mix)动画功能可以很柔和过度两个不同的动画,但在混合时期,稍有不善,非常容易出现各种错乱.在Spine2D骨骼动画群上,有人提出全K帧. ...
- Android Context 上下文 你必须知道的一切
本文转载于:http://blog.csdn.net/lmj623565791/article/details/40481055 转载请标明出处:http://blog.csdn.net/lmj623 ...
- 在chrome 总调试cordova出现Detached from the target. Remote debugging has been terminated with reason: Connection lost. Please re-attach to the new target
在chrome 总调试cordova出现如下错误: "Detached from the target. Remote debugging has been terminated with ...