Background: MySQL database and Redis running on the host (Linux), server operating within docker, web operating within Nginx (Nginx runs in the docker), acquired user IP is 10.0.0.10 similar docker internal IP
Demand: get real IP
method:
A, Nginx modify the configuration file, the docker container] [/etc/nginx/conf.d/default.conf
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /web; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # location /api/ { HTTP proxy_pass: // Base-Framework-Server /; Set # Below is done to obtain the real IP proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_redirect default; } }
Second, the increase in [tools] HttpUtil
import org.springframework.util.StringUtils; import javax.servlet.http.HttpServletRequest; /** * @ClassName HttpUtil * @Description Http Tools * @Author AAFE (WeChat: xskdjs945) * @Date 2019/11/28 10:19 * @Version 1.0 **/ public class HttpUtil { /** * Get real client IP * * @param request * @return */ public static String getClientIP(HttpServletRequest request) { // Nginx need to set configuration String request.getHeader IP = ( " X-Real-the IP- " ); IF (StringUtils.isEmpty (IP)) { ip = request.getRemoteAddr(); } return ip; } }