diff --git a/docs/user-guide/nginx-configuration/configmap.md b/docs/user-guide/nginx-configuration/configmap.md index e43090fb33..688c8012c5 100755 --- a/docs/user-guide/nginx-configuration/configmap.md +++ b/docs/user-guide/nginx-configuration/configmap.md @@ -32,6 +32,8 @@ The following table shows a configuration option's name, type, and the default v |[hide-headers](#hide-headers)|string array|empty| |[access-log-params](#access-log-params)|string|""| |[access-log-path](#access-log-path)|string|"/var/log/nginx/access.log"| +|[http-access-log-path](#http-access-log-path)|string|""| +|[stream-access-log-path](#stream-access-log-path)|string|""| |[enable-access-log-for-default-backend](#enable-access-log-for-default-backend)|bool|"false"| |[error-log-path](#error-log-path)|string|"/var/log/nginx/error.log"| |[enable-modsecurity](#enable-modsecurity)|bool|"false"| @@ -207,10 +209,24 @@ _References:_ ## access-log-path -Access log path. Goes to `/var/log/nginx/access.log` by default. +Access log path for both http and stream context. Goes to `/var/log/nginx/access.log` by default. __Note:__ the file `/var/log/nginx/access.log` is a symlink to `/dev/stdout` +## http-access-log-path + +Access log path for http context globally. +_**default:**_ "" + +__Note:__ If not specified, the `access-log-path` will be used. + +## stream-access-log-path + +Access log path for stream context globally. +_**default:**_ "" + +__Note:__ If not specified, the `access-log-path` will be used. + ## enable-access-log-for-default-backend Enables logging access to default backend. _**default:**_ is disabled. diff --git a/internal/ingress/controller/config/config.go b/internal/ingress/controller/config/config.go index b39145957f..5f164e7465 100644 --- a/internal/ingress/controller/config/config.go +++ b/internal/ingress/controller/config/config.go @@ -111,11 +111,20 @@ type Configuration struct { // By default this is disabled EnableAccessLogForDefaultBackend bool `json:"enable-access-log-for-default-backend"` - // AccessLogPath sets the path of the access logs if enabled + // AccessLogPath sets the path of the access logs for both http and stream contexts if enabled // http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log + // http://nginx.org/en/docs/stream/ngx_stream_log_module.html#access_log // By default access logs go to /var/log/nginx/access.log AccessLogPath string `json:"access-log-path,omitempty"` + // HttpAccessLogPath sets the path of the access logs for http context globally if enabled + // http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log + HttpAccessLogPath string `json:"http-access-log-path,omitempty"` + + // StreamAccessLogPath sets the path of the access logs for stream context globally if enabled + // http://nginx.org/en/docs/stream/ngx_stream_log_module.html#access_log + StreamAccessLogPath string `json:"stream-access-log-path,omitempty"` + // WorkerCPUAffinity bind nginx worker processes to CPUs this will improve response latency // http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity // By default this is disabled diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index db9b2097a8..29546a9895 100755 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -331,7 +331,7 @@ http { {{ if $cfg.EnableSyslog }} access_log syslog:server={{ $cfg.SyslogHost }}:{{ $cfg.SyslogPort }} upstreaminfo if=$loggable; {{ else }} - access_log {{ $cfg.AccessLogPath }} upstreaminfo {{ $cfg.AccessLogParams }} if=$loggable; + access_log {{ or $cfg.HttpAccessLogPath $cfg.AccessLogPath }} upstreaminfo {{ $cfg.AccessLogParams }} if=$loggable; {{ end }} {{ end }} @@ -697,7 +697,7 @@ stream { {{ if $cfg.DisableAccessLog }} access_log off; {{ else }} - access_log {{ $cfg.AccessLogPath }} log_stream {{ $cfg.AccessLogParams }}; + access_log {{ or $cfg.StreamAccessLogPath $cfg.AccessLogPath }} log_stream {{ $cfg.AccessLogParams }}; {{ end }} error_log {{ $cfg.ErrorLogPath }}; diff --git a/test/e2e/settings/access_log.go b/test/e2e/settings/access_log.go new file mode 100644 index 0000000000..0e4c1d8279 --- /dev/null +++ b/test/e2e/settings/access_log.go @@ -0,0 +1,87 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package settings + +import ( + "strings" + + "github.com/onsi/ginkgo" + "k8s.io/ingress-nginx/test/e2e/framework" +) + +var _ = framework.DescribeSetting("access-log", func() { + f := framework.NewDefaultFramework("access-log") + + ginkgo.Context("access-log-path", func() { + + ginkgo.It("use the default configuration", func() { + f.WaitForNginxConfiguration( + func(cfg string) bool { + return strings.Contains(cfg, "access_log /var/log/nginx/access.log upstreaminfo") && + strings.Contains(cfg, "access_log /var/log/nginx/access.log log_stream") + }) + }) + + ginkgo.It("use the specified configuration", func() { + f.UpdateNginxConfigMapData("access-log-path", "/tmp/access.log") + f.WaitForNginxConfiguration( + func(cfg string) bool { + return strings.Contains(cfg, "access_log /tmp/access.log upstreaminfo") && + strings.Contains(cfg, "access_log /tmp/access.log log_stream") + }) + }) + }) + + ginkgo.Context("http-access-log-path", func() { + + ginkgo.It("use the specified configuration", func() { + f.UpdateNginxConfigMapData("http-access-log-path", "/tmp/http-access.log") + f.WaitForNginxConfiguration( + func(cfg string) bool { + return strings.Contains(cfg, "access_log /tmp/http-access.log upstreaminfo") && + strings.Contains(cfg, "access_log /var/log/nginx/access.log log_stream") + }) + }) + }) + + ginkgo.Context("stream-access-log-path", func() { + + ginkgo.It("use the specified configuration", func() { + f.UpdateNginxConfigMapData("stream-access-log-path", "/tmp/stream-access.log") + f.WaitForNginxConfiguration( + func(cfg string) bool { + return strings.Contains(cfg, "access_log /tmp/stream-access.log log_stream") && + strings.Contains(cfg, "access_log /var/log/nginx/access.log upstreaminfo") + }) + }) + }) + + ginkgo.Context("http-access-log-path & stream-access-log-path", func() { + + ginkgo.It("use the specified configuration", func() { + f.SetNginxConfigMapData(map[string]string{ + "http-access-log-path": "/tmp/http-access.log", + "stream-access-log-path": "/tmp/stream-access.log", + }) + f.WaitForNginxConfiguration( + func(cfg string) bool { + return strings.Contains(cfg, "access_log /tmp/http-access.log upstreaminfo") && + strings.Contains(cfg, "access_log /tmp/stream-access.log log_stream") + }) + }) + }) +})