Skip to content

Commit c525f56

Browse files
authored
Merge pull request #213 from kongdewen/dewen/adding-main-snippet
Adding main snippets to allow Main context customization
2 parents 3b38473 + d1825ea commit c525f56

File tree

8 files changed

+22
-0
lines changed

8 files changed

+22
-0
lines changed

examples/customization/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The table below summarizes some of the options. More options (extensions) are av
3232
| 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`|
3333
| 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`|
3434
| `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`|
35+
| N/A | `main-snippets` | Sets a custom snippet in main context. | N/A |
3536
| N/A | `http-snippets` | Sets a custom snippet in http context. | N/A |
3637
| `nginx.org/location-snippets` | `location-snippets` | Sets a custom snippet in location context. | N/A |
3738
| `nginx.org/server-snippets` | `server-snippets` | Sets a custom snippet in server context. | N/A |

examples/customization/nginx-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ data:
3232
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
3333
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
3434
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
35+
main-snippets: | # No default. Pipe is used for multiple line snippets.
36+
load_module "modules/ngx_http_geoip_module.so";
37+
load_module "modules/ngx_stream_module.so";
3538
http-snippets: | # Pipe is used for multiple line snippets. Make sure the snippet is not a default value, in order to avoid duplication.
3639
map $uri $new_uri {
3740
/old.html /index.html;

nginx-controller/controller/controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,13 @@ func (lbc *LoadBalancerController) syncCfgm(task Task) {
554554
cfg.ProxyMaxTempFileSize = proxyMaxTempFileSize
555555
}
556556

557+
if mainMainSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "main-snippets", cfgm, "\n"); exists {
558+
if err != nil {
559+
glog.Error(err)
560+
} else {
561+
cfg.MainMainSnippets = mainMainSnippets
562+
}
563+
}
557564
if mainHTTPSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "http-snippets", cfgm, "\n"); exists {
558565
if err != nil {
559566
glog.Error(err)

nginx-controller/nginx/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Config struct {
1111
HTTP2 bool
1212
RedirectToHTTPS bool
1313
SSLRedirect bool
14+
MainMainSnippets []string
1415
MainHTTPSnippets []string
1516
MainServerNamesHashBucketSize string
1617
MainServerNamesHashMaxSize string

nginx-controller/nginx/configurator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ func (cnf *Configurator) updatePlusEndpoints(ingEx *IngressEx) error {
694694
func (cnf *Configurator) UpdateConfig(config *Config, ingExes []*IngressEx) error {
695695
cnf.config = config
696696
mainCfg := &NginxMainConfig{
697+
MainSnippets: config.MainMainSnippets,
697698
HTTPSnippets: config.MainHTTPSnippets,
698699
ServerNamesHashBucketSize: config.MainServerNamesHashBucketSize,
699700
ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize,

nginx-controller/nginx/nginx.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type NginxMainConfig struct {
106106
ServerNamesHashMaxSize string
107107
LogFormat string
108108
HealthStatus bool
109+
MainSnippets []string
109110
HTTPSnippets []string
110111
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
111112
SSLProtocols string

nginx-controller/nginx/templates/nginx-plus.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ daemon off;
1111
error_log /var/log/nginx/error.log notice;
1212
pid /var/run/nginx.pid;
1313

14+
{{- if .MainSnippets}}
15+
{{range $value := .MainSnippets}}
16+
{{$value}}{{end}}
17+
{{- end}}
1418

1519
events {
1620
worker_connections 1024;

nginx-controller/nginx/templates/nginx.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ daemon off;
1010
error_log /var/log/nginx/error.log warn;
1111
pid /var/run/nginx.pid;
1212

13+
{{- if .MainSnippets}}
14+
{{range $value := .MainSnippets}}
15+
{{$value}}{{end}}
16+
{{- end}}
1317

1418
events {
1519
worker_connections 1024;

0 commit comments

Comments
 (0)