Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 50x html for error page #4164

Merged
merged 29 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,14 @@ reload: default
install: default
$(INSTALL) -d /usr/local/apisix/
$(INSTALL) -d /usr/local/apisix/logs/
$(INSTALL) -d /usr/local/apisix/html/
$(INSTALL) -d /usr/local/apisix/conf/cert
$(INSTALL) conf/mime.types /usr/local/apisix/conf/mime.types
$(INSTALL) conf/config.yaml /usr/local/apisix/conf/config.yaml
$(INSTALL) conf/config-default.yaml /usr/local/apisix/conf/config-default.yaml
$(INSTALL) conf/debug.yaml /usr/local/apisix/conf/debug.yaml
$(INSTALL) conf/cert/* /usr/local/apisix/conf/cert/
$(INSTALL) html/* /usr/local/apisix/html/

$(INSTALL) -d $(INST_LUADIR)/apisix
$(INSTALL) apisix/*.lua $(INST_LUADIR)/apisix/
Expand Down
57 changes: 57 additions & 0 deletions apisix/cli/html_page.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

return [=[
<!--
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
-->
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>500 Internal Server Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>You can report issue to <a href="https://github.com/apache/apisix/issues">APISIX</a></p>
<p><em>Faithfully yours, <a href="https://apisix.apache.org/">APISIX</a>.</em></p>
</body>
</html>
]=]
36 changes: 36 additions & 0 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ http {
include mime.types;
charset utf-8;

# error_page
error_page 500 @50x.html;

{% if real_ip_header then %}
real_ip_header {* real_ip_header *};
{% print("\nDeprecated: apisix.real_ip_header has been moved to nginx_config.http.real_ip_header. apisix.real_ip_header will be removed in the future version. Please use nginx_config.http.real_ip_header first.\n\n") %}
Expand Down Expand Up @@ -290,6 +293,17 @@ http {
apisix.http_control()
}
}

location @50x.html {
try_files /50x.html $uri;
header_filter_by_lua_block {
apisix.http_header_filter_phase()
}

log_by_lua_block {
apisix.http_log_phase()
}
}
}
{% end %}

Expand Down Expand Up @@ -367,6 +381,17 @@ http {
apisix.http_admin()
}
}

location @50x.html {
try_files /50x.html $uri;
header_filter_by_lua_block {
apisix.http_header_filter_phase()
}

log_by_lua_block {
apisix.http_log_phase()
}
}
}
{% end %}

Expand Down Expand Up @@ -617,6 +642,17 @@ http {
proxy_pass $upstream_mirror_host$request_uri;
}
{% end %}

location @50x.html {
try_files /50x.html $uri;
header_filter_by_lua_block {
apisix.http_header_filter_phase()
}

log_by_lua_block {
apisix.http_log_phase()
}
}
}
# http end configuration snippet starts
{% if http_end_configuration_snippet then %}
Expand Down
9 changes: 9 additions & 0 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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")
Expand Down Expand Up @@ -586,6 +587,11 @@ Please modify "admin_key" in conf/config.yaml .
if not ok then
util.die("failed to update nginx.conf: ", err, "\n")
end

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


Expand All @@ -605,6 +611,9 @@ local function start(env, ...)
local cmd_logs = "mkdir -p " .. env.apisix_home .. "/logs"
util.execute_cmd(cmd_logs)

local cmd_html = "mkdir -p " .. env.apisix_home .. "/html"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add it to gitignore:

conf/nginx.conf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Please review it again.

util.execute_cmd(cmd_html)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adding an empty line here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Please review it again.

-- check running
local pid_path = env.apisix_home .. "/logs/nginx.pid"
local pid = util.read_file(pid_path)
Expand Down
11 changes: 9 additions & 2 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,13 @@ function _M.http_access_phase()

set_upstream_host(api_ctx)

ngx_var.ctx_ref = ctxdump.stash_ngx_ctx()
tokers marked this conversation as resolved.
Show resolved Hide resolved
local up_scheme = api_ctx.upstream_scheme
if up_scheme == "grpcs" or up_scheme == "grpc" then
ngx_var.ctx_ref = ctxdump.stash_ngx_ctx()
return ngx.exec("@grpc_pass")
end

if api_ctx.dubbo_proxy_enabled then
ngx_var.ctx_ref = ctxdump.stash_ngx_ctx()
return ngx.exec("@dubbo_pass")
end
end
Expand Down Expand Up @@ -534,6 +533,10 @@ end


function _M.http_header_filter_phase()
if not ngx.ctx or not ngx.ctx.api_ctx then
ngx.ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stash ngx.ctx unconditionally but checking the existence of ngx.ctx , this behavior leaks some keys for ngx.ctx tables (for those requests which doesn't make internal redirect).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Please review it again.

end

core.response.set_header("Server", ver_header)

local up_status = get_var("upstream_status")
Expand Down Expand Up @@ -620,6 +623,10 @@ end

function _M.http_log_phase()
local api_ctx = common_phase("log")
if not api_ctx then
return
end

healthcheck_passive(api_ctx)

if api_ctx.server_picker and api_ctx.server_picker.after_balance then
Expand Down
24 changes: 24 additions & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ _EOC_
lua_socket_log_errors off;
client_body_buffer_size 8k;

error_page 500 \@50x.html;

upstream apisix_backend {
server 0.0.0.1;
balancer_by_lua_block {
Expand Down Expand Up @@ -426,6 +428,17 @@ _EOC_
more_clear_headers Date;
}

location \@50x.html {
try_files /50x.html \$uri;
header_filter_by_lua_block {
apisix.http_header_filter_phase()
}

log_by_lua_block {
apisix.http_log_phase()
}
}

location = /v3/auth/authenticate {
content_by_lua_block {
ngx.log(ngx.WARN, "etcd auth failed!")
Expand Down Expand Up @@ -512,6 +525,17 @@ _EOC_
}
}

location \@50x.html {
try_files /50x.html \$uri;
header_filter_by_lua_block {
apisix.http_header_filter_phase()
}

log_by_lua_block {
apisix.http_log_phase()
}
}

location /v1/ {
content_by_lua_block {
apisix.http_control()
Expand Down
141 changes: 141 additions & 0 deletions t/error_page/error_page.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use t::APISIX 'no_plan';

log_level('debug');
repeat_each(1);
no_long_string();
no_root_location();

run_tests;

__DATA__

tokers marked this conversation as resolved.
Show resolved Hide resolved
=== TEST 1: set route with serverless-post-function plugin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"serverless-post-function": {
"functions" : ["return function() if ngx.var.http_x_test_status ~= nil then;ngx.exit(tonumber(ngx.var.http_x_test_status));end;end"]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/*"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 2: test apisix with internal error code 500
--- request
GET /hello
--- more_headers
X-Test-Status: 500
--- error_code: 500
--- response_body_like
.*apisix.apache.org.*



=== TEST 3: test apisix with internal error code 502
--- request
GET /hello
--- more_headers
X-Test-Status: 502
--- error_code: 502
--- response_body eval
qr/502 Bad Gateway/



=== TEST 4: test apisix with internal error code 503
--- request
GET /hello
--- more_headers
X-Test-Status: 503
--- error_code: 503
--- response_body eval
qr/503 Service Unavailable/



=== TEST 5: test apisix with internal error code 504
--- request
GET /hello
--- more_headers
X-Test-Status: 504
--- error_code: 504
--- response_body eval
qr/504 Gateway Time-out/



=== TEST 6: test apisix with upstream error code 500
--- request
GET /specific_status
--- more_headers
X-Test-Upstream-Status: 500
--- error_code: 500
--- response_body
upstream status: 500



=== TEST 7: delete route(id: 1)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, message = t('/apisix/admin/routes/1',
ngx.HTTP_DELETE,
nil,
[[{
"action": "delete"
}]]
)
ngx.say("[delete] code: ", code, " message: ", message)
}
}
--- request
GET /t
--- response_body
[delete] code: 200 message: passed
--- no_error_log
[error]
Loading