nginx反向代理后sring boot获取不到真实IP
程序员文章站
2022-07-09 19:48:24
现在的项目都是vue+spring boot前后端分离,vue放在nginx下,为了避免跨域大家都会用nginx将请求后端的接口用nginx反向代理,这样会导致springboot获取不到真实IP;需要改下面代码1.nginx配置location /ElensBackEnd/ { proxy_pass http://localhost:18081/ElensBackEnd/; proxy_set_header Host $host;...
现在的项目都是vue+spring boot前后端分离,vue放在nginx下,为了避免跨域大家都会用nginx将请求后端的接口用nginx反向代理,这样会导致springboot获取不到真实IP;需要改下面代码
1.nginx配置
location /ElensBackEnd/ {
proxy_pass http://localhost:18081/ElensBackEnd/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 1000m;
}
java代码如下:
package com.Util;
import lombok.extern.java.Log;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
@Log
public class IpUtil {
/***
* 获取客户端ip地址(可以穿透代理)
* @param request
* @return
*/
public static String getClientIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
log.info("X-Forwarded-For:" + ip);
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
log.info("Proxy-Client-IP:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
log.info("WL-Proxy-Client-IP:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
log.info("HTTP_X_FORWARDED_FOR:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED");
log.info("HTTP_X_FORWARDED:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");
log.info("HTTP_X_CLUSTER_CLIENT_IP:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
log.info("HTTP_CLIENT_IP:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_FORWARDED_FOR");
log.info("HTTP_FORWARDED_FOR:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_FORWARDED");
log.info("HTTP_FORWARDED:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_VIA");
log.info("HTTP_VIA:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("REMOTE_ADDR");
log.info("REMOTE_ADDR:" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
log.info("getRemoteAddr:" + ip);
}
return ip;
}
}
重点:我在windows上面的时候nginx监听端口是8080没问题,8081就会有问题,简直有毒;希望遇到的小伙伴绕过此坑。
server {
listen 8080;
server_name localhost;
本文地址:https://blog.csdn.net/wangxingwu1/article/details/107383173
推荐阅读
-
.Net Core/Framework之Nginx反向代理后获取客户端IP等数据探索
-
nginx反向代理后abp的webapi host如何获取客户端ip?
-
Nginx代理后服务端使用remote_addr获取真实IP
-
Nginx 反向代理获取真实IP问题
-
nginx反向代理后sring boot获取不到真实IP
-
nginx经过多层代理后获取真实来源ip过程详解
-
PHP、Tomcat获取Nginx代理后的客户端真实IP
-
nginx反向代理后abp的webapi host如何获取客户端ip?
-
.Net Core/Framework之Nginx反向代理后获取客户端IP等数据探索
-
通过nginx反向代理,Tomcat获取真实的客户端IP而非服务器IP nginx tomcat 慢 nginx代理tomcat nginx tomcat 40