Skip to content

Commit

Permalink
Merge pull request #213 from kongdewen/dewen/adding-main-snippet
Browse files Browse the repository at this point in the history
Adding main snippets to allow Main context customization
  • Loading branch information
pleshakov authored Nov 16, 2017
2 parents 3b38473 + d1825ea commit c525f56
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/customization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
3 changes: 3 additions & 0 deletions examples/customization/nginx-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions nginx-controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions nginx-controller/nginx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Config struct {
HTTP2 bool
RedirectToHTTPS bool
SSLRedirect bool
MainMainSnippets []string
MainHTTPSnippets []string
MainServerNamesHashBucketSize string
MainServerNamesHashMaxSize string
Expand Down
1 change: 1 addition & 0 deletions nginx-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions nginx-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions nginx-controller/nginx/templates/nginx-plus.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions nginx-controller/nginx/templates/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c525f56

Please sign in to comment.