From 332dee10f18ca84d315e32833a7105ce1e6eebcd Mon Sep 17 00:00:00 2001 From: ben Date: Sat, 16 Jan 2021 16:47:16 +0100 Subject: [PATCH 1/2] [#1295] Add feature redirect if host does not exist By default a 404 is returned if none of the hosts match. One could define default-server-redirect-url in a configMap to redirect to another URL --- .../configuration/global-configuration/configmap-resource.md | 4 ++++ internal/configs/config_params.go | 2 ++ internal/configs/configmaps.go | 5 +++++ internal/configs/version1/config.go | 1 + internal/configs/version1/nginx-plus.tmpl | 2 +- internal/configs/version1/nginx.tmpl | 2 +- 6 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs-web/configuration/global-configuration/configmap-resource.md b/docs-web/configuration/global-configuration/configmap-resource.md index ca19005e97..f36ade8d70 100644 --- a/docs-web/configuration/global-configuration/configmap-resource.md +++ b/docs-web/configuration/global-configuration/configmap-resource.md @@ -114,6 +114,10 @@ See the doc about [VirtualServer and VirtualServerRoute resources](/nginx-ingres - Enables or disables the `real_ip_recursive `_ directive. - ``False`` - + * - ``default-server-return`` + - Configures the `return `_ directive in the default server, which handles a client request if none of the hosts of Ingress or VirtualServer resources match. The default value configures NGINX to return a 404 error page. You can configure a fixed response or a redirect. For example, ``default-server-return: 302 https://nginx.org`` will redirect a client to https://nginx.org. + - ``404`` + - * - ``server-tokens`` - Enables or disables the `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`` diff --git a/internal/configs/config_params.go b/internal/configs/config_params.go index 14d65f2f90..199d37abc9 100644 --- a/internal/configs/config_params.go +++ b/internal/configs/config_params.go @@ -7,6 +7,7 @@ import conf_v1alpha1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configurat type ConfigParams struct { ClientMaxBodySize string DefaultServerAccessLogOff bool + DefaultServerReturn string FailTimeout string HealthCheckEnabled bool HealthCheckMandatory bool @@ -131,6 +132,7 @@ type Listener struct { // NewDefaultConfigParams creates a ConfigParams with default values. func NewDefaultConfigParams() *ConfigParams { return &ConfigParams{ + DefaultServerReturn: "404", ServerTokens: "on", ProxyConnectTimeout: "60s", ProxyReadTimeout: "60s", diff --git a/internal/configs/configmaps.go b/internal/configs/configmaps.go index eba87a132a..2a8f5bf79f 100644 --- a/internal/configs/configmaps.go +++ b/internal/configs/configmaps.go @@ -246,6 +246,10 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool) *Con } } + if defaultServerReturn, exists := cfgm.Data["default-server-return"]; exists { + cfgParams.DefaultServerReturn = defaultServerReturn + } + if proxyBuffering, exists, err := GetMapKeyAsBool(cfgm.Data, "proxy-buffering", cfgm); exists { if err != nil { glog.Error(err) @@ -497,6 +501,7 @@ func GenerateNginxMainConfig(staticCfgParams *StaticConfigParams, config *Config nginxCfg := &version1.MainConfig{ AccessLogOff: config.MainAccessLogOff, DefaultServerAccessLogOff: config.DefaultServerAccessLogOff, + DefaultServerReturn: config.DefaultServerReturn, ErrorLogLevel: config.MainErrorLogLevel, HealthStatus: staticCfgParams.HealthStatus, HealthStatusURI: staticCfgParams.HealthStatusURI, diff --git a/internal/configs/version1/config.go b/internal/configs/version1/config.go index ac9f160385..8cba0c643f 100644 --- a/internal/configs/version1/config.go +++ b/internal/configs/version1/config.go @@ -145,6 +145,7 @@ type Location struct { type MainConfig struct { AccessLogOff bool DefaultServerAccessLogOff bool + DefaultServerReturn string ErrorLogLevel string HealthStatus bool HealthStatusURI string diff --git a/internal/configs/version1/nginx-plus.tmpl b/internal/configs/version1/nginx-plus.tmpl index 4a7f72a9dc..01d632d7eb 100644 --- a/internal/configs/version1/nginx-plus.tmpl +++ b/internal/configs/version1/nginx-plus.tmpl @@ -148,7 +148,7 @@ http { {{end}} location / { - return 404; + return {{.DefaultServerReturn}}; } } diff --git a/internal/configs/version1/nginx.tmpl b/internal/configs/version1/nginx.tmpl index 8d43f4038a..6035ad09cb 100644 --- a/internal/configs/version1/nginx.tmpl +++ b/internal/configs/version1/nginx.tmpl @@ -133,7 +133,7 @@ http { {{end}} location / { - return 404; + return {{.DefaultServerReturn}}; } } From a818be534f58972317013ff2a98e4d5a0bdd8d78 Mon Sep 17 00:00:00 2001 From: Ben van B <030@users.noreply.github.com> Date: Fri, 22 Jan 2021 18:41:26 +0100 Subject: [PATCH 2/2] Update docs-web/configuration/global-configuration/configmap-resource.md Co-authored-by: Jodie Putrino --- .../configuration/global-configuration/configmap-resource.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs-web/configuration/global-configuration/configmap-resource.md b/docs-web/configuration/global-configuration/configmap-resource.md index f36ade8d70..9fbf82ae5b 100644 --- a/docs-web/configuration/global-configuration/configmap-resource.md +++ b/docs-web/configuration/global-configuration/configmap-resource.md @@ -115,7 +115,7 @@ See the doc about [VirtualServer and VirtualServerRoute resources](/nginx-ingres - ``False`` - * - ``default-server-return`` - - Configures the `return `_ directive in the default server, which handles a client request if none of the hosts of Ingress or VirtualServer resources match. The default value configures NGINX to return a 404 error page. You can configure a fixed response or a redirect. For example, ``default-server-return: 302 https://nginx.org`` will redirect a client to https://nginx.org. + - Configures the `return `_ directive in the default server, which handles a client request if none of the hosts of Ingress or VirtualServer resources match. The default value configures NGINX to return a 404 error page. You can configure a fixed response or a redirect. For example, ``default-server-return: 302 https://nginx.org`` will redirect a client to ``https://nginx.org``. - ``404`` - * - ``server-tokens``