首先,在nginx配置中添加如下配置
server { ???listen ??????80; ???server_name ?www.wenki.info; ???#要访问的域名 ???charset utf8; ???location / { ???????proxy_pass ??????http://wenki_info; ???????proxy_set_header Host ?????$host; ???????proxy_set_header X-Real-IP $remote_addr; ???????proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ???}}
使用如下命令重新加载配置
nginx -s reload
服务端获取ip地址代码
public static String realIP(HttpServletRequest request) { ???String xff = request.getHeader("x-forwarded-for"); ???if (xff != null) { ???????int index = xff.indexOf(‘,‘); ???????if (index != -1) { ???????????xff = xff.substring(0, index); ???????} ???????return xff.trim(); ???} ???return request.getRemoteAddr();}
服务器使用nginx做代理,通过HttpServletRequest获取请求用户真实IP地址
原文地址:http://www.cnblogs.com/wenhui92/p/7594493.html