微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx代理,tomcat部署服务器,后端获取客户端真实i

1、环境部署说明

后端部署在tomcat服务器上,前端用@R_404_5171@做代理访问

tomcat部署目录

nginx代理,tomcat部署服务器,后端获取客户端真实i

@R_404_5171@配置:

upstream wcfront{
    server  localhost:8991;//后台接口
}

server {
    listen       8998;//h6访问接口
    server_name  192.168.2.37;
    charset utf-8;

    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;



     location ^~ /fs/ {
        alias  /var/zkbc/fs/;
    }

   
    location = / {
        root   /opt/nlcn/backend/wcfront/www;//h6页面路径
        index  index.html index.htm;
    }

    location = /index {
        root   /opt/nlcn/backend/wcfront/www;
        rewrite ^(.*) /;
    }

    location ~ .*\.(html)$ {
        root   /opt/nlcn/backend/wcfront/www;
        index  index.html index.htm;
    }

     location ~ .*(css)$ {
        root   /opt/nlcn/backend/wcfront/www;
        index  index.html index.htm;
    }
    

    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|woff|woff2|svg|ttf)$ {
        root   /opt/nlcn/backend/wcfront/www;
        index  index.html index.htm;
    }

    location / {
        proxy_pass http://wcfront;
        proxy_set_header Host $host:$server_port;
    }

}

2、当前配置后端获取ip一直未127.0.0.1,现在需求是能够获取客户端的ip

(1)在@R_404_5171@配置上加如下配置,注意:在location /下加

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

(2)重启@R_404_5171@

@R_404_5171@ -s reload

ps -ef | grep @R_404_5171@ 可以查看@R_404_5171@的启动状态及启动时间

(3)配置tomcat

service.xml 下找到  pattern="%h %l %u %t "%r" %s %b" />  

把 %h 修改成 %{X-Real-IP}i 

重启服务

(4) java获取客户端ip的方式

public static String getIpAddress(HttpServletRequest request) {
        String ip = null;

        //X-Forwarded-For:Squid 服务代理
        String ipAddresses = request.getHeader("X-Forwarded-For");

        if (ipAddresses == null || ipAddresses.length() == 0 || "unkNown".equalsIgnoreCase(ipAddresses)) {
            //Proxy-Client-IP:apache 服务代理
            ipAddresses = request.getHeader("Proxy-Client-IP");
        }

        if (ipAddresses == null || ipAddresses.length() == 0 || "unkNown".equalsIgnoreCase(ipAddresses)) {
            //WL-Proxy-Client-IP:weblogic 服务代理
            ipAddresses = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ipAddresses == null || ipAddresses.length() == 0 || "unkNown".equalsIgnoreCase(ipAddresses)) {
            //HTTP_CLIENT_IP:有些代理服务器
            ipAddresses = request.getHeader("HTTP_CLIENT_IP");
        }

        if (ipAddresses == null || ipAddresses.length() == 0 || "unkNown".equalsIgnoreCase(ipAddresses)) {
            //X-Real-IP:@R_404_5171@服务代理
            ipAddresses = request.getHeader("X-Real-IP");
        }

        //有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
        if (ipAddresses != null && ipAddresses.length() != 0) {
            ip = ipAddresses.split(",")[0];
        }

        //还是不能获取到,最后再通过request.getRemoteAddr();获取
        if (ip == null || ip.length() == 0 || "unkNown".equalsIgnoreCase(ipAddresses)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

3、结论

需要注意的是这种方式获取的是客户端所在网络的外网地址,而不是客户端的真实ip。

例如

多个终端都在同一个局域网访问,获取的ip为同一个网关地址。

手机4g访问,获取的ip也不是手机的实际ip,而是网络ip,例如获取的ip是61.158.147.109,但实际手机ip是61.158.147.*(同网段另外一个ip地址),不是特别清楚手机ip和109什么关系,有兴趣的朋友可以研究研究。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐