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 16 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
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 502 503 504 /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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a named location or internal location.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

root html;
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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

root html;
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 {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better to use named location?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really good to do that.Because we will use "50x.html" to search in the "html" directory.

Copy link
Member

Choose a reason for hiding this comment

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

So the log phase might get a rewritten URL instead of the original one? This is not good news...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I found a new way. We can use "try_files" to get the html.

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.

root html;
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
12 changes: 10 additions & 2 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,12 @@ function _M.http_access_phase()
set_upstream_host(api_ctx)

local up_scheme = api_ctx.upstream_scheme
ngx_var.ctx_ref = ctxdump.stash_ngx_ctx()
tokers marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -620,6 +619,15 @@ end

function _M.http_log_phase()
local api_ctx = common_phase("log")
if not api_ctx then
local ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)
Copy link
Member

Choose a reason for hiding this comment

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

Why not reapply the ctx in http_header_filter_phase?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good. Let me fix it.

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.

api_ctx = ctx and ctx.api_ctx
end

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
38 changes: 38 additions & 0 deletions html/50x.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--
Copy link
Member

Choose a reason for hiding this comment

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

Can we put this file into a Lua script like ngx_tpl.lua? So that there is no need to create an extra directory.

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.

#
# 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>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>
31 changes: 31 additions & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ 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("html/50x.html");
$user_yaml_config = <<_EOC_;
apisix:
node_listen: 1984
Expand Down Expand Up @@ -374,6 +375,8 @@ _EOC_
lua_socket_log_errors off;
client_body_buffer_size 8k;

error_page 500 502 503 504 /50x.html;

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

location = /50x.html {
internal;
root html;

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 +528,19 @@ _EOC_
}
}

location = /50x.html {
internal;
root html;

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 Expand Up @@ -625,6 +654,8 @@ $ssl_ecc_key
$test2_crt
>>> ../conf/cert/test2.key
$test2_key
>>> 50x.html
$test_50x_html
$user_apisix_yaml
_EOC_

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_like
.*apisix.apache.org.*



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



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



=== 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]
9 changes: 9 additions & 0 deletions t/lib/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ function _M.plugin_proxy_rewrite_args()
end


function _M.specific_status()
local status = ngx.var.http_x_test_upstream_status
if status ~= nil then
ngx.status = status
ngx.say("upstream status: ", status)
end
end


function _M.status()
ngx.say("ok")
end
Expand Down
4 changes: 2 additions & 2 deletions t/node/not-exist-upstream.t
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ GET /not_found
--- request
GET /hello
--- error_code_like: ^(?:50\d)$
--- response_body eval
qr/502 Bad Gateway|500 Internal Server Error/
--- response_body_like
.*apisix.apache.org.*
--- grep_error_log eval
qr/\[error\].*/
--- grep_error_log_out eval
Expand Down
5 changes: 3 additions & 2 deletions t/node/timeout-upstream.t
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ passed
--- request
GET /sleep1
--- error_code: 504
--- response_body eval
qr/504 Gateway Time-out/
--- response_body_like
.*apisix.apache.org.*

--- error_log
timed out) while reading response header from upstream
Loading