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

1、环境部署说明

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

tomcat部署目录

image.png

nginx配置:

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

server {
    listen       8998;//h5访问接口
    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;//h5页面路径
        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)在nginx配置上加如下配置,注意:在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)重启nginx

nginx -s reload

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

(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:nginx服务代理
            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什么关系,有兴趣的朋友可以研究研究。

猜你喜欢

转载自blog.51cto.com/1385903/2114705