diff --git a/application.go b/application.go index b4c962e..4628731 100644 --- a/application.go +++ b/application.go @@ -357,14 +357,18 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) { defer func() { //是否展示日志 if that.WebConnectLog != nil { - ipStr := Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":")) + //负载均衡优化 - if ipStr == "127.0.0.1" { - if req.Header.Get("X-Forwarded-For") != "" { - ipStr = req.Header.Get("X-Forwarded-For") - } else if req.Header.Get("X-Real-IP") != "" { - ipStr = req.Header.Get("X-Real-IP") - } + ipStr := "" + if req.Header.Get("X-Forwarded-For") != "" { + ipStr = req.Header.Get("X-Forwarded-For") + } else if req.Header.Get("X-Real-IP") != "" { + ipStr = req.Header.Get("X-Real-IP") + } + //负载均衡优化 + if ipStr == "" { + //RemoteAddr := that.Req.RemoteAddr + ipStr = Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":")) } that.WebConnectLog.Infoln(ipStr, context.Req.Method, diff --git a/context.go b/context.go index 9d23f22..df14cd7 100644 --- a/context.go +++ b/context.go @@ -75,14 +75,17 @@ func (that *Context) View() { if that.Session("user_id").Data != nil { that.Log["user_id"] = that.Session("user_id").ToCeilInt() } - ipStr := Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":")) //负载均衡优化 - if ipStr == "127.0.0.1" { - if that.Req.Header.Get("X-Forwarded-For") != "" { - ipStr = that.Req.Header.Get("X-Forwarded-For") - } else if that.Req.Header.Get("X-Real-IP") != "" { - ipStr = that.Req.Header.Get("X-Real-IP") - } + ipStr := "" + if that.Req.Header.Get("X-Forwarded-For") != "" { + ipStr = that.Req.Header.Get("X-Forwarded-For") + } else if that.Req.Header.Get("X-Real-IP") != "" { + ipStr = that.Req.Header.Get("X-Real-IP") + } + //负载均衡优化 + if ipStr == "" { + //RemoteAddr := that.Req.RemoteAddr + ipStr = Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":")) } that.Log["ip"] = ipStr that.Db.Insert("logs", that.Log)