有时候网站通过nginx作为前端反向代理,这样后端直接获取ip地址就是代理服务器的IP地址,一般是一个内网IP,下面这段代码可以帮助你获取访客的真实IP地址,当然必须nginx前端做了相应的参数传递处理。
static public string GetClientIP() { string result = HttpContext.Current.Request.ServerVariables['HTTP_X_FORWARDED_FOR']; if (null == result || result == String.Empty) { result = HttpContext.Current.Request.ServerVariables['REMOTE_ADDR']; } if (null == result || result == String.Empty) { result = HttpContext.Current.Request.UserHostAddress; } return result; }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。