浏览代码

use x-forwarded-for in nginx log parse if so desired

Tobias von Dewitz 6 年之前
父节点
当前提交
bed5afdb48
共有 1 个文件被更改,包括 12 次插入0 次删除
  1. 12 0
      main.go

+ 12 - 0
main.go

@@ -278,6 +278,18 @@ func nginxLogCapture(logfile, format string) {
 			continue
 		}
 
+		if *useXForwardedAsSource {
+			xff, err := logEntry.Field("http_x_forwarded_for")
+			if err != nil {
+				remote = xff
+			}
+		}
+
+		if remote == "" {
+			log.Println("remote is empty: ignoring request.")
+			continue
+		}
+
 		// only use the first host in case there are multiple hosts in the log
 		if cidx := strings.Index(remote, ","); cidx >= 0 {
 			remote = remote[0:cidx]