From 8f75e811e281c3d9e6249df5eee83574e049181e Mon Sep 17 00:00:00 2001 From: spacewander Date: Thu, 29 Jul 2021 12:01:14 +0800 Subject: [PATCH] fix: when a request caused a 500 error, the status was converted to 405 It happened when this request is not a GET or HEAD request. Signed-off-by: spacewander --- .gitignore | 1 - apisix/cli/ngx_tpl.lua | 12 ++++-- apisix/cli/ops.lua | 9 ----- .../{cli/html_page.lua => error_handling.lua} | 19 ++++++++-- t/APISIX.pm | 11 +++--- t/error_page/50x.html | 38 ------------------- t/error_page/error_page.t | 18 +++++++-- 7 files changed, 45 insertions(+), 63 deletions(-) rename apisix/{cli/html_page.lua => error_handling.lua} (84%) delete mode 100644 t/error_page/50x.html diff --git a/.gitignore b/.gitignore index 3697d8d54910..c06fa2ce3169 100644 --- a/.gitignore +++ b/.gitignore @@ -77,4 +77,3 @@ boofuzz-results/ # release tar package *.tgz release/* -html/* diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua index 0816faef9633..69dcfe7d224c 100644 --- a/apisix/cli/ngx_tpl.lua +++ b/apisix/cli/ngx_tpl.lua @@ -352,7 +352,9 @@ http { location @50x.html { set $from_error_page 'true'; - try_files /50x.html $uri; + content_by_lua_block { + require("apisix.error_handling").handle_500() + } } } {% end %} @@ -434,7 +436,9 @@ http { location @50x.html { set $from_error_page 'true'; - try_files /50x.html $uri; + content_by_lua_block { + require("apisix.error_handling").handle_500() + } } } {% end %} @@ -683,7 +687,9 @@ http { location @50x.html { set $from_error_page 'true'; - try_files /50x.html $uri; + content_by_lua_block { + require("apisix.error_handling").handle_500() + } header_filter_by_lua_block { apisix.http_header_filter_phase() } diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 73166a6a13d8..4c1c46af651c 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -19,7 +19,6 @@ local etcd = require("apisix.cli.etcd") local util = require("apisix.cli.util") local file = require("apisix.cli.file") local ngx_tpl = require("apisix.cli.ngx_tpl") -local html_page = require("apisix.cli.html_page") local profile = require("apisix.core.profile") local template = require("resty.template") local argparse = require("argparse") @@ -683,14 +682,6 @@ Please modify "admin_key" in conf/config.yaml . if not ok then util.die("failed to update nginx.conf: ", err, "\n") end - - local cmd_html = "mkdir -p " .. env.apisix_home .. "/html" - util.execute_cmd(cmd_html) - - local ok, err = util.write_file(env.apisix_home .. "/html/50x.html", html_page) - if not ok then - util.die("failed to write 50x.html: ", err, "\n") - end end diff --git a/apisix/cli/html_page.lua b/apisix/error_handling.lua similarity index 84% rename from apisix/cli/html_page.lua rename to apisix/error_handling.lua index f262ad8c226c..524c46b2c7a7 100644 --- a/apisix/cli/html_page.lua +++ b/apisix/error_handling.lua @@ -14,9 +14,12 @@ -- See the License for the specific language governing permissions and -- limitations under the License. -- +local core = require("apisix.core") +local ngx = ngx -return [=[ - + +local _M = {} +local page_500 = [[ @@ -35,5 +38,13 @@ return [=[

You can report issue to APISIX

Faithfully yours, APISIX.

- -]=] +]] + + +function _M.handle_500() + core.response.set_header("Content-Type", "text/html") + ngx.say(page_500) +end + + +return _M diff --git a/t/APISIX.pm b/t/APISIX.pm index e1c5557b0078..4c615a8bbd13 100644 --- a/t/APISIX.pm +++ b/t/APISIX.pm @@ -89,7 +89,6 @@ my $ssl_ecc_crt = read_file("t/certs/apisix_ecc.crt"); my $ssl_ecc_key = read_file("t/certs/apisix_ecc.key"); my $test2_crt = read_file("t/certs/test2.crt"); my $test2_key = read_file("t/certs/test2.key"); -my $test_50x_html = read_file("t/error_page/50x.html"); $user_yaml_config = <<_EOC_; apisix: node_listen: 1984 @@ -541,7 +540,9 @@ _EOC_ location \@50x.html { set \$from_error_page 'true'; - try_files /50x.html \$uri; + content_by_lua_block { + require("apisix.error_handling").handle_500() + } header_filter_by_lua_block { apisix.http_header_filter_phase() } @@ -627,7 +628,9 @@ _EOC_ location \@50x.html { set \$from_error_page 'true'; - try_files /50x.html \$uri; + content_by_lua_block { + require("apisix.error_handling").handle_500() + } header_filter_by_lua_block { apisix.http_header_filter_phase() } @@ -774,8 +777,6 @@ $ssl_ecc_key $test2_crt >>> ../conf/cert/test2.key $test2_key ->>> 50x.html -$test_50x_html $user_apisix_yaml _EOC_ diff --git a/t/error_page/50x.html b/t/error_page/50x.html deleted file mode 100644 index f3b9e209fd3f..000000000000 --- a/t/error_page/50x.html +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - -500 Internal Server Error - - - -

An error occurred.

-

You can report issue to APISIX

-

Faithfully yours, APISIX.

- - diff --git a/t/error_page/error_page.t b/t/error_page/error_page.t index 6eb0fa725157..70cc34c68af9 100644 --- a/t/error_page/error_page.t +++ b/t/error_page/error_page.t @@ -118,7 +118,19 @@ upstream status: 500 -=== TEST 7: delete route(id: 1) +=== TEST 7: test apisix with internal error code 500, method isn't GET or HEAD +--- request +POST /hello +123 +--- more_headers +X-Test-Status: 500 +--- error_code: 500 +--- response_body_like +.*apisix.apache.org.* + + + +=== TEST 8: delete route(id: 1) --- config location /t { content_by_lua_block { @@ -142,7 +154,7 @@ GET /t -=== TEST 8: set route which upstream is blocking +=== TEST 9: set route which upstream is blocking --- config location /t { content_by_lua_block { @@ -175,7 +187,7 @@ passed -=== TEST 9: client abort +=== TEST 10: client abort --- request GET /mysleep?seconds=3 --- abort