码迷,mamicode.com
首页 > Web开发 > 详细

服务器使用nginx做代理,通过HttpServletRequest获取请求用户真实IP地址

时间:2017-09-26 01:00:18      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:reload   服务器   war   int   public   request   color   serve   indexof   

首先,在nginx配置中添加如下配置

server {
    listen       80;
    server_name  www.wenki.info;    #要访问的域名

    charset utf8;

    location / {
        proxy_pass       http://wenki_info;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

使用如下命令重新加载配置

nginx -s reload

 

服务端获取ip地址代码

public static String realIP(HttpServletRequest request) {
    String xff = request.getHeader("x-forwarded-for");
    if (xff != null) {
        int index = xff.indexOf(‘,‘);
        if (index != -1) {
            xff = xff.substring(0, index);
        }
        return xff.trim();
    }
    return request.getRemoteAddr();
}

 

服务器使用nginx做代理,通过HttpServletRequest获取请求用户真实IP地址

标签:reload   服务器   war   int   public   request   color   serve   indexof   

原文地址:http://www.cnblogs.com/wenhui92/p/7594493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!