前后端分离,nginx代理java后端获取不到客户端的真实ip

解决的办法是在nginx配置中,location节点中加入: 

        proxy_set_header Host $host; 

        proxy_set_header X-Real-IP $remote_addr; 

        proxy_set_header REMOTE-HOST $remote_addr; 

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

加完以后差不多这个样子
 

server {
    listen 443;
        server_name www.*.com;
        root /opt;
        index index.html index.php;
        
        location /api {
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header REMOTE-HOST $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_pass http://127.0.0.1:8087;
        }
} 

 

你可能感兴趣的:(nginx,java)