From 87b1e83274d251262d736f45055d1c274b8d9d22 Mon Sep 17 00:00:00 2001 From: Qi Date: Mon, 9 Oct 2023 12:12:23 +0800 Subject: [PATCH] fix(template): fix failure of starting if `proxy_access_log` is `off` (cherry picked from commit 6e4c25bd3fc8079519bf4d9c6056b5cb136b4fee) --- kong/cmd/utils/prefix_handler.lua | 1 + kong/templates/nginx_kong.lua | 5 +++++ spec/01-unit/04-prefix_handler_spec.lua | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/kong/cmd/utils/prefix_handler.lua b/kong/cmd/utils/prefix_handler.lua index 88170a5cae6a..540b442e60b9 100644 --- a/kong/cmd/utils/prefix_handler.lua +++ b/kong/cmd/utils/prefix_handler.lua @@ -239,6 +239,7 @@ local function compile_conf(kong_config, conf_template) -- computed config properties for templating local compile_env = { _escape = ">", + proxy_access_log_enabled = kong_config.proxy_access_log ~= "off", pairs = pairs, ipairs = ipairs, tostring = tostring, diff --git a/kong/templates/nginx_kong.lua b/kong/templates/nginx_kong.lua index a3393870fa01..9209e5b7fcd1 100644 --- a/kong/templates/nginx_kong.lua +++ b/kong/templates/nginx_kong.lua @@ -81,7 +81,12 @@ server { # https://github.com/Kong/lua-kong-nginx-module#lua_kong_error_log_request_id lua_kong_error_log_request_id $kong_request_id; +> if proxy_access_log_enabled then access_log ${{PROXY_ACCESS_LOG}} kong_log_format; +> else + access_log off; +> end + error_log ${{PROXY_ERROR_LOG}} ${{LOG_LEVEL}}; > if proxy_ssl_enabled then diff --git a/spec/01-unit/04-prefix_handler_spec.lua b/spec/01-unit/04-prefix_handler_spec.lua index f6bc6a733705..f156428b2209 100644 --- a/spec/01-unit/04-prefix_handler_spec.lua +++ b/spec/01-unit/04-prefix_handler_spec.lua @@ -496,6 +496,16 @@ describe("NGINX conf compiler", function() local nginx_conf = prefix_handler.compile_kong_stream_conf(conf) assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf) + local conf = assert(conf_loader(nil, { + proxy_access_log = "off", + stream_listen = "0.0.0.0:9100", + nginx_stream_tcp_nodelay = "on", + })) + local nginx_conf = prefix_handler.compile_kong_conf(conf) + assert.matches("access_log%soff;", nginx_conf) + local nginx_conf = prefix_handler.compile_kong_stream_conf(conf) + assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf) + local conf = assert(conf_loader(nil, { proxy_stream_access_log = "/dev/stdout custom", stream_listen = "0.0.0.0:9100",