1 一定要把proxy_pass語句放在最后面
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;
????????client_max_body_size 1024m;
????????# 一定要把這句放在后面
????????proxy_pass http://test_app;
}文章來源:http://www.zghlxwxcb.cn/news/detail-637556.html
2 使用代碼獲取,以java為例文章來源地址http://www.zghlxwxcb.cn/news/detail-637556.html
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { // 多次反向代理后會有多個ip值,第一個ip才是真實ip if (ip.contains(",")) { ip = ip.split(",")[0]; } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } //如果上面沒有獲得代理的IP,則使用獲取真實IP的請求頭進行嘗試 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } //上面都還沒有,有可能就是局域網內的IP進行操作的,則獲取請求頭的局域網IP if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); }
到了這里,關于nginx獲取不到真實ip地址,注意這個細節(jié)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!