nginx如何获取真实客户端ip

nginx作为反向代理服务器,即代理我们的服务端,下面介绍下如何配置nginx获取真实的客户端ip

1、配置nginx.con

bash 复制代码
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           proxy_pass http://127.0.0.1:8080;
           root   html;
           index  index.html index.htm;

        #获取真实ip配置
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

2、在java程序中可以通过如下方式获取:

java 复制代码
    @GetMapping(value = "/getRealIp")
    public String getRealIp(){
        log.info(request.getRemoteAddr());
        log.info(request.getRequestURL().toString());
        log.info("端口" + request.getRemotePort() + "");


        log.info("======================");
        log.info("真实ip:{}",getClientIP());

        return request.getRemoteAddr() + "\n" +
                request.getRequestURL();

    }

 /***
     * 获取客户端IP地址;这里通过了Nginx获取;X-Real-IP
     */
    public String getClientIP() {
        String fromSource = "X-Real-IP";
        String ip = request.getHeader("X-Real-IP");
        log.info("X-Real-IP 初始化值的ip:{} ", ip);
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
            fromSource = "X-Forwarded-For";
            log.info("X-Forwarded-For赋值成功ip:{} ", ip);
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            fromSource = "Proxy-Client-IP";
            log.info("Proxy-Client-IP赋值成功ip:{} ", ip);
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            fromSource = "WL-Proxy-Client-IP";
            log.info("WL-Proxy-Client-IP赋值成功ip:{} ", ip);
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            fromSource = "request.getRemoteAddr";
            log.info("RemoteAddr赋值成功ip:{} ", ip);
//        }
        return ip;
    }

这样就可以打印出真实ip了!即request.getHeader("X-Real-IP")的值

引用:

查看端口占用及释放所占用的端口_查询谷歌浏览器的端口号_JDSYDWR的博客-CSDN博客

https://blog.51cto.com/u_14020077/5836635

【网络】为什么百度查到的ip和ipconfig查到的不一样;详解公网Ip和私网ip;详解网络分类ABC;_wx5bc47e97d0ded的技术博客_51CTO博客

相关推荐
路在脚下@7 分钟前
Spring如何处理循环依赖
java·后端·spring
一个不秃头的 程序员30 分钟前
代码加入SFTP JAVA ---(小白篇3)
java·python·github
丁总学Java41 分钟前
--spring.profiles.active=prod
java·spring
苹果醋31 小时前
React系列(八)——React进阶知识点拓展
运维·vue.js·spring boot·nginx·课程设计
上等猿1 小时前
集合stream
java
我要出家当道士1 小时前
Nginx单向链表 ngx_list_t
数据结构·nginx·链表·c
java1234_小锋1 小时前
MyBatis如何处理延迟加载?
java·开发语言
菠萝咕噜肉i1 小时前
MyBatis是什么?为什么有全自动ORM框架还是MyBatis比较受欢迎?
java·mybatis·框架·半自动
林的快手1 小时前
209.长度最小的子数组
java·数据结构·数据库·python·算法·leetcode
xserver22 小时前
ensp 基于EASY IP的公司出口链路配置
网络·tcp/ip·智能路由器