使用nginx代理,怎么获取真实的IP
1.在nginx.conf配置中加入配置信息
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
?
2.然后再后臺(tái)獲取真實(shí)IP
public static String getIpAddr(HttpServletRequest request) {
? ? ? ? String ip = null;
? ? ? ? try {
? ? ? ? ? ? if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip) || LOCALHOST.equalsIgnoreCase(ip)) {
? ? ? ? ? ? ? ? ip = request.getHeader("HTTP_X_FORWARDED_FOR");
? ? ? ? ? ? }
? ? ? ? ? ? if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip) || LOCALHOST.equalsIgnoreCase(ip)) {
? ? ? ? ? ? ? ? ip = request.getHeader("X-Forwarded-For");
? ? ? ? ? ? }
? ? ? ? ? ? if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip) || LOCALHOST.equalsIgnoreCase(ip)) {
? ? ? ? ? ? ? ? ip = request.getHeader("X-Real-IP");
? ? ? ? ? ? }
? ? ? ? ? ? if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip) || LOCALHOST.equalsIgnoreCase(ip)) {
? ? ? ? ? ? ? ? ip = request.getRemoteAddr();
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? logger.error("IPUtils ERROR ", e);
? ? ? ? }
?
? ? ? ? // ? ? ? ?//使用代理,則獲取第一個(gè)IP地址
? ? ? ? // ? ? ? ?if(StringUtils.isEmpty(ip) && ip.length() > 15) {
? ? ? ? // ? ? ? ? ?if(ip.indexOf(",") > 0) {
? ? ? ? // ? ? ? ? ? ? ?ip = ip.substring(0, ip.indexOf(","));
? ? ? ? // ? ? ? ? ?}
? ? ? ? // ? ? ?}
?
? ? ? ? return ip;
? ? }
?
?
總結(jié)
以上是生活随笔為你收集整理的使用nginx代理,怎么获取真实的IP的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows 端微信多开
- 下一篇: Eclipse Maven 编译错误 D