diff --git a/nginx-controller/controller/controller.go b/nginx-controller/controller/controller.go index 01f03d9c9f..002e7fc275 100644 --- a/nginx-controller/controller/controller.go +++ b/nginx-controller/controller/controller.go @@ -329,6 +329,9 @@ func (lbc *LoadBalancerController) syncCfgm(key string) { glog.Errorf("In configmap %v/%v 'http2' contains invalid declaration: %v, ignoring", cfgm.Namespace, cfgm.Name, err) } } + if logFormat, exists := cfgm.Data["log-format"]; exists { + cfg.MainLogFormat = logFormat + } } lbc.cnf.UpdateConfig(cfg) diff --git a/nginx-controller/nginx/config.go b/nginx-controller/nginx/config.go index 79cc0d41e3..d514829821 100644 --- a/nginx-controller/nginx/config.go +++ b/nginx-controller/nginx/config.go @@ -8,6 +8,7 @@ type Config struct { HTTP2 bool MainServerNamesHashBucketSize string MainServerNamesHashMaxSize string + MainLogFormat string } // NewDefaultConfig creates a Config with default values diff --git a/nginx-controller/nginx/configurator.go b/nginx-controller/nginx/configurator.go index 3b1a18a3e2..9e92516991 100644 --- a/nginx-controller/nginx/configurator.go +++ b/nginx-controller/nginx/configurator.go @@ -329,6 +329,7 @@ func (cnf *Configurator) UpdateConfig(config *Config) { mainCfg := &NginxMainConfig{ ServerNamesHashBucketSize: config.MainServerNamesHashBucketSize, ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize, + LogFormat: config.MainLogFormat, } cnf.nginx.UpdateMainConfigFile(mainCfg) diff --git a/nginx-controller/nginx/nginx.conf.tmpl b/nginx-controller/nginx/nginx.conf.tmpl index c5803b323d..13cdf4ed0f 100644 --- a/nginx-controller/nginx/nginx.conf.tmpl +++ b/nginx-controller/nginx/nginx.conf.tmpl @@ -15,10 +15,13 @@ http { include /etc/nginx/mime.types; default_type application/octet-stream; + {{if .LogFormat -}} + log_format main '{{.LogFormat}}'; + {{- else -}} log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; - + {{- end }} access_log /var/log/nginx/access.log main; sendfile on; diff --git a/nginx-controller/nginx/nginx.go b/nginx-controller/nginx/nginx.go index bb73a0919c..91bb815730 100644 --- a/nginx-controller/nginx/nginx.go +++ b/nginx-controller/nginx/nginx.go @@ -3,7 +3,7 @@ package nginx import ( "bytes" "fmt" - "html/template" + "text/template" "os" "os/exec" "path" @@ -62,6 +62,7 @@ type Location struct { type NginxMainConfig struct { ServerNamesHashBucketSize string ServerNamesHashMaxSize string + LogFormat string } // NewUpstreamWithDefaultServer creates an upstream with the default server.