diff --git a/examples/customization/README.md b/examples/customization/README.md index 9c812836bb..2cb4b521f5 100644 --- a/examples/customization/README.md +++ b/examples/customization/README.md @@ -32,6 +32,7 @@ The table below summarizes some of the options. More options (extensions) are av | N/A | `real-ip-header` | Sets the value of the [real_ip_header](http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_header) directive. | `X-Real-IP`| | N/A | `real-ip-recursive` | Enables or disables the [real_ip_recursive](http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_recursive) directive. | `False`| | `nginx.org/server-tokens` | `server-tokens` | Enables or disables the [server_tokens](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens) directive. Additionally, with the NGINX Plus, you can specify a custom string value, including the empty string value, which disables the emission of the “Server” field. | `True`| +| N/A | `main-snippets` | Sets a custom snippet in main context. | N/A | | N/A | `http-snippets` | Sets a custom snippet in http context. | N/A | | `nginx.org/location-snippets` | `location-snippets` | Sets a custom snippet in location context. | N/A | | `nginx.org/server-snippets` | `server-snippets` | Sets a custom snippet in server context. | N/A | diff --git a/examples/customization/nginx-config.yaml b/examples/customization/nginx-config.yaml index 9e2daafcbd..e7285b6ee2 100644 --- a/examples/customization/nginx-config.yaml +++ b/examples/customization/nginx-config.yaml @@ -32,6 +32,9 @@ data: real-ip-header: "proxy_protocol" # default is X-Real-IP. Sets the value of the real_ip_header directive. http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_header real-ip-recursive: "True" # default is "False". Enables or disables the real_ip_recursive directive. See http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_recursive server-tokens: "False" # default is "True". Enables or disables the server_tokens directive. See http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens + main-snippets: | # No default. Pipe is used for multiple line snippets. + load_module "modules/ngx_http_geoip_module.so"; + load_module "modules/ngx_stream_module.so"; http-snippets: | # Pipe is used for multiple line snippets. Make sure the snippet is not a default value, in order to avoid duplication. map $uri $new_uri { /old.html /index.html; diff --git a/nginx-controller/controller/controller.go b/nginx-controller/controller/controller.go index cfe351c604..48caffa069 100644 --- a/nginx-controller/controller/controller.go +++ b/nginx-controller/controller/controller.go @@ -554,6 +554,13 @@ func (lbc *LoadBalancerController) syncCfgm(task Task) { cfg.ProxyMaxTempFileSize = proxyMaxTempFileSize } + if mainMainSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "main-snippets", cfgm, "\n"); exists { + if err != nil { + glog.Error(err) + } else { + cfg.MainMainSnippets = mainMainSnippets + } + } if mainHTTPSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "http-snippets", cfgm, "\n"); exists { if err != nil { glog.Error(err) diff --git a/nginx-controller/nginx/config.go b/nginx-controller/nginx/config.go index dd7e835880..3c7f18c05e 100644 --- a/nginx-controller/nginx/config.go +++ b/nginx-controller/nginx/config.go @@ -11,6 +11,7 @@ type Config struct { HTTP2 bool RedirectToHTTPS bool SSLRedirect bool + MainMainSnippets []string MainHTTPSnippets []string MainServerNamesHashBucketSize string MainServerNamesHashMaxSize string diff --git a/nginx-controller/nginx/configurator.go b/nginx-controller/nginx/configurator.go index 9e4b036dfb..b6b137434d 100644 --- a/nginx-controller/nginx/configurator.go +++ b/nginx-controller/nginx/configurator.go @@ -694,6 +694,7 @@ func (cnf *Configurator) updatePlusEndpoints(ingEx *IngressEx) error { func (cnf *Configurator) UpdateConfig(config *Config, ingExes []*IngressEx) error { cnf.config = config mainCfg := &NginxMainConfig{ + MainSnippets: config.MainMainSnippets, HTTPSnippets: config.MainHTTPSnippets, ServerNamesHashBucketSize: config.MainServerNamesHashBucketSize, ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize, diff --git a/nginx-controller/nginx/nginx.go b/nginx-controller/nginx/nginx.go index 4bcf0d1fac..94dc25d7f4 100644 --- a/nginx-controller/nginx/nginx.go +++ b/nginx-controller/nginx/nginx.go @@ -106,6 +106,7 @@ type NginxMainConfig struct { ServerNamesHashMaxSize string LogFormat string HealthStatus bool + MainSnippets []string HTTPSnippets []string // http://nginx.org/en/docs/http/ngx_http_ssl_module.html SSLProtocols string diff --git a/nginx-controller/nginx/templates/nginx-plus.tmpl b/nginx-controller/nginx/templates/nginx-plus.tmpl index dbf66541a5..1abb86a2cf 100644 --- a/nginx-controller/nginx/templates/nginx-plus.tmpl +++ b/nginx-controller/nginx/templates/nginx-plus.tmpl @@ -11,6 +11,10 @@ daemon off; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; +{{- if .MainSnippets}} +{{range $value := .MainSnippets}} +{{$value}}{{end}} +{{- end}} events { worker_connections 1024; diff --git a/nginx-controller/nginx/templates/nginx.tmpl b/nginx-controller/nginx/templates/nginx.tmpl index 1cf0d635c9..cd9d603f5c 100644 --- a/nginx-controller/nginx/templates/nginx.tmpl +++ b/nginx-controller/nginx/templates/nginx.tmpl @@ -10,6 +10,10 @@ daemon off; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; +{{- if .MainSnippets}} +{{range $value := .MainSnippets}} +{{$value}}{{end}} +{{- end}} events { worker_connections 1024;