后端想获取远程客户端的动态ip地址不能直接获取到,因为在远程客户端请求过程中会通过各层的网关及层层代理经过一系列请求后获取不到真实ip了。不过我们可以通过Nginx来获取远程地址然后转发代理到后端应用,此时可以拿到用户的远程ip地址。
1、安装Nginx依赖:
yum install -y pcre pcre-devel
yum install -y openssl openssl-devel
2、去Nginx官网下载Nginx的tar包,然后解压编译:
./configure --prefix=/usr/local/nginx-1.15.6 --with-http_stub_status_module --with-httpssl_module --with-http_realip_module
3、编辑Nginx配置文件,配置代理应用的请求头:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;