Skip to content
This repository was archived by the owner on Apr 17, 2019. It is now read-only.

Commit 6f9e61d

Browse files
authored
Merge pull request #1239 from aledbf/conditional-log
[nginx-ingress-controller]: Add support for conditional log of urls
2 parents 413b7dc + b8cc9d0 commit 6f9e61d

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

ingress/controllers/nginx/nginx.tmpl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ http {
7676
'[$proxy_add_x_forwarded_for] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" '
7777
'$request_length $request_time $upstream_addr $upstream_response_length $upstream_response_time $upstream_status';
7878

79-
access_log /var/log/nginx/access.log upstreaminfo;
79+
{{/* map urls that should not appear in access.log */}}
80+
{{/* http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log */}}
81+
map $request $loggable {
82+
{{- range $reqUri := $cfg.skipAccessLogUrls }}
83+
{{ $reqUri }} 0;{{ end }}
84+
default 1;
85+
}
86+
87+
access_log /var/log/nginx/access.log upstreaminfo if=$loggable;
8088
error_log /var/log/nginx/error.log {{ $cfg.errorLogLevel }};
8189

8290
{{ if not (empty .defResolver) }}# Custom dns resolver.

ingress/controllers/nginx/nginx/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ type Configuration struct {
162162
// http://nginx.org/en/docs/http/ngx_http_core_module.html#server_names_hash_bucket_size
163163
ServerNameHashBucketSize int `structs:"server-name-hash-bucket-size,omitempty"`
164164

165+
// SkipAccessLogURLs sets a list of URLs that should not appear in the NGINX access log
166+
// This is useful with urls like `/health` or `health-check` that make "complex" reading the logs
167+
// By default this list is empty
168+
SkipAccessLogURLs []string `structs:"skip-access-log-urls,-"`
169+
165170
// Enables or disables the redirect (301) to the HTTPS port
166171
SSLRedirect bool `structs:"ssl-redirect,omitempty"`
167172

@@ -275,6 +280,7 @@ func NewDefault() Configuration {
275280
UseHTTP2: true,
276281
CustomHTTPErrors: make([]int, 0),
277282
WhitelistSourceRange: make([]string, 0),
283+
SkipAccessLogURLs: make([]string, 0),
278284
}
279285

280286
if glog.V(5) {

ingress/controllers/nginx/nginx/utils.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import (
3333
)
3434

3535
const (
36-
customHTTPErrors = "custom-http-errors"
36+
customHTTPErrors = "custom-http-errors"
37+
skipAccessLogUrls = "skip-access-log-urls"
3738
)
3839

3940
// getDNSServers returns the list of nameservers located in the file /etc/resolv.conf
@@ -110,6 +111,12 @@ func (ngx *Manager) ReadConfig(conf *api.ConfigMap) config.Configuration {
110111
}
111112
}
112113

114+
cSkipUrls := make([]string, 0)
115+
if val, ok := conf.Data[skipAccessLogUrls]; ok {
116+
delete(conf.Data, skipAccessLogUrls)
117+
cSkipUrls = strings.Split(val, ",")
118+
}
119+
113120
err = decoder.Decode(conf.Data)
114121
if err != nil {
115122
glog.Infof("%v", err)
@@ -135,6 +142,7 @@ func (ngx *Manager) ReadConfig(conf *api.ConfigMap) config.Configuration {
135142
}
136143

137144
cfgDefault.CustomHTTPErrors = ngx.filterErrors(cErrors)
145+
cfgDefault.SkipAccessLogURLs = cSkipUrls
138146
return cfgDefault
139147
}
140148

0 commit comments

Comments
 (0)