diff --git a/changelog/unreleased/kong/inject-nginx-directives-location.yml b/changelog/unreleased/kong/inject-nginx-directives-location.yml new file mode 100644 index 000000000000..2e0a19e72c63 --- /dev/null +++ b/changelog/unreleased/kong/inject-nginx-directives-location.yml @@ -0,0 +1,3 @@ +message: Allow to inject Nginx directives into Kong's proxy location block +type: feature +scope: Configuration diff --git a/kong.conf.default b/kong.conf.default index c904d64a60d6..7bd463da33d5 100644 --- a/kong.conf.default +++ b/kong.conf.default @@ -1069,6 +1069,8 @@ # - `nginx_http_`: Injects `` in Kong's `http {}` block. # - `nginx_proxy_`: Injects `` in Kong's proxy # `server {}` block. +# - `nginx_location_`: Injects `` in Kong's proxy `/` +# location block (nested under Kong's proxy server {} block). # - `nginx_upstream_`: Injects `` in Kong's proxy # `upstream {}` block. # - `nginx_admin_`: Injects `` in Kong's Admin API diff --git a/kong/conf_loader/init.lua b/kong/conf_loader/init.lua index 9b04ed7a9fe2..29ac8d52a2f4 100644 --- a/kong/conf_loader/init.lua +++ b/kong/conf_loader/init.lua @@ -197,6 +197,11 @@ local DYNAMIC_KEY_NAMESPACES = { prefix = "nginx_proxy_", ignore = EMPTY, }, + { + injected_conf_name = "nginx_location_directives", + prefix = "nginx_location_", + ignore = EMPTY, + }, { injected_conf_name = "nginx_status_directives", prefix = "nginx_status_", diff --git a/kong/templates/nginx_kong.lua b/kong/templates/nginx_kong.lua index 7e9a04bb4f93..c12ba4b3f82e 100644 --- a/kong/templates/nginx_kong.lua +++ b/kong/templates/nginx_kong.lua @@ -157,6 +157,11 @@ server { proxy_buffering on; proxy_request_buffering on; + # injected nginx_location_* directives +> for _, el in ipairs(nginx_location_directives) do + $(el.name) $(el.value); +> end + proxy_set_header TE $upstream_te; proxy_set_header Host $upstream_host; proxy_set_header Upgrade $upstream_upgrade; diff --git a/spec/01-unit/04-prefix_handler_spec.lua b/spec/01-unit/04-prefix_handler_spec.lua index 0337917237a4..7cc4d9c56769 100644 --- a/spec/01-unit/04-prefix_handler_spec.lua +++ b/spec/01-unit/04-prefix_handler_spec.lua @@ -611,6 +611,14 @@ describe("NGINX conf compiler", function() assert.matches("large_client_header_buffers%s+16 24k;", nginx_conf) end) + it("injects nginx_location_* directives", function() + local conf = assert(conf_loader(nil, { + nginx_location_proxy_ignore_headers = "X-Accel-Redirect", + })) + local nginx_conf = prefix_handler.compile_kong_conf(conf) + assert.matches("proxy_ignore_headers%sX%-Accel%-Redirect;", nginx_conf) + end) + it("injects nginx_admin_* directives", function() local conf = assert(conf_loader(nil, { nginx_admin_large_client_header_buffers = "4 24k",