Merge pull request #3780 from arturxx8/master

Enable access log for default backend
This commit is contained in:
Kubernetes Prow Robot 2019-02-26 05:51:39 -08:00 committed by GitHub
commit ec632817ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 119 additions and 71 deletions

View file

@ -32,6 +32,7 @@ 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"|
|[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-dynamic-tls-records](#enable-dynamic-tls-records)|bool|"true"|
|[enable-modsecurity](#enable-modsecurity)|bool|"false"|
@ -184,6 +185,10 @@ Access log path. Goes to `/var/log/nginx/access.log` by default.
__Note:__ the file `/var/log/nginx/access.log` is a symlink to `/dev/stdout`
## enable-access-log-for-default-backend
Enables logging access to default backend. _**default:**_ is disabled.
## error-log-path
Error log path. Goes to `/var/log/nginx/error.log` by default.

View file

@ -99,6 +99,10 @@ type Configuration struct {
// By default it's empty
AccessLogParams string `json:"access-log-params,omitempty"`
// EnableAccessLogForDefaultBackend enable access_log for default backend
// By default this is disabled
EnableAccessLogForDefaultBackend bool `json:"enable-access-log-for-default-backend"`
// AccessLogPath sets the path of the access logs if enabled
// http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
// By default access logs go to /var/log/nginx/access.log
@ -598,6 +602,7 @@ func NewDefault() Configuration {
AllowBackendServerHeader: false,
AccessLogPath: "/var/log/nginx/access.log",
AccessLogParams: "",
EnableAccessLogForDefaultBackend: false,
WorkerCPUAffinity: "",
ErrorLogPath: "/var/log/nginx/error.log",
BlockCIDRs: defBlockEntity,

View file

@ -18,6 +18,7 @@ package controller
import (
"fmt"
"k8s.io/ingress-nginx/internal/ingress/annotations/log"
"sort"
"strconv"
"strings"
@ -928,6 +929,10 @@ func (n *NGINXController) createServers(data []*ingress.Ingress,
Backend: du.Name,
Proxy: ngxProxy,
Service: du.Service,
Logs: log.Config{
Access: n.store.GetBackendConfiguration().EnableAccessLogForDefaultBackend,
Rewrite: false,
},
},
}}

View file

@ -615,6 +615,8 @@ http {
{{ if $IsIPV6Enabled }}listen [::]:{{ $all.ListenPorts.Default }} default_server {{ if $all.Cfg.ReusePort }}reuseport{{ end }} backlog={{ $all.BacklogSize }};{{ end }}
set $proxy_upstream_name "internal";
access_log off;
location / {
return 404;
}

View file

@ -98,4 +98,35 @@ var _ = framework.IngressNginxDescribe("Default backend", func() {
Expect(resp.StatusCode).Should(Equal(test.Status))
}
})
It("enables access logging for default backend", func() {
f.UpdateNginxConfigMapData("enable-access-log-for-default-backend", "true")
host := "foo"
resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)+"/somethingOne").
Set("Host", host).
End()
Expect(len(errs)).Should(Equal(0))
Expect(resp.StatusCode).Should(Equal(http.StatusNotFound))
logs, err := f.NginxLogs()
Expect(err).ToNot(HaveOccurred())
Expect(logs).To(ContainSubstring("/somethingOne"))
})
It("disables access logging for default backend", func() {
f.UpdateNginxConfigMapData("enable-access-log-for-default-backend", "false")
host := "bar"
resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)+"/somethingTwo").
Set("Host", host).
End()
Expect(len(errs)).Should(Equal(0))
Expect(resp.StatusCode).Should(Equal(http.StatusNotFound))
logs, err := f.NginxLogs()
Expect(err).ToNot(HaveOccurred())
Expect(logs).ToNot(ContainSubstring("/somethingTwo"))
})
})