Play framework+nginx反向代理无法获得remoteAddress解决办法

今天碰到一个问题,要在play里面判断客户端ip,从而判断用户来源,用nginx做反向代理,结果无论如何得到的都是127.0.0.1的地址

后来查了一些资料,记录如下:

1、play的application.conf的配置文件要加入一行:XForwardedSupport=127.0.0.1,10.0.0.25

When using an HTTP frontal server, request addresses are seen as coming from the HTTP server. In a usual set-up, where you both have the Play app and the proxy running on the same machine, the Play app will see the requests coming from 127.0.0.1.

Proxy servers can add a specific header to the request to tell the proxied application where the request came from. Most web servers will add an X-Forwarded-For header with the remote client IP address as first argument. If you enable the forward support in your Play app configuration:

XForwardedSupport=127.0.0.1,10.0.0.25
官方说法就是前端服务器会加入X-Forwarded-For这个参数,play就可以从前端服务器获取客户端ip地址了

不过光这些还不行

2、nginx配置文件还要在location里面加入

location / {
index  index.html index.htm index.jsp index.jspx index.do;
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_pass       http://127.0.0.1:9000;
client_max_body_size    1000m;  //上传文件设置最大可以上传1g档案
}

ok,问题解决,收工~~~

Leave a Comment