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 all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ ci/openwhisk-utilities/
# release tar package
*.tgz
release/*
html/*
Copy link
Member

Choose a reason for hiding this comment

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

To make the license check pass, also need to change

# Exclude CI files

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.

39 changes: 39 additions & 0 deletions apisix/cli/html_page.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--
-- 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 [=[
<!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>
]=]
39 changes: 39 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,18 @@ http {
apisix.http_control()
}
}

location @50x.html {
set $from_error_page 'true';
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 +382,18 @@ http {
apisix.http_admin()
}
}

location @50x.html {
set $from_error_page 'true';
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 +644,18 @@ http {
proxy_pass $upstream_mirror_host$request_uri;
}
{% end %}

location @50x.html {
set $from_error_page 'true';
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,14 @@ 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


Expand Down
19 changes: 17 additions & 2 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -475,26 +475,27 @@ 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


function _M.dubbo_access_phase()
ngx.ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)
ngx_var.ctx_ref = ''
end


function _M.grpc_access_phase()
ngx.ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)
ngx_var.ctx_ref = ''

local api_ctx = ngx.ctx.api_ctx
if not api_ctx then
Expand Down Expand Up @@ -534,6 +535,16 @@ end


function _M.http_header_filter_phase()
if ngx_var.ctx_ref ~= '' then
-- prevent for the table leak
local stash_ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)

-- internal redirect, so we should apply the ctx
if ngx_var.from_error_page == "true" then
ngx.ctx = stash_ctx
end
end

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

local up_status = get_var("upstream_status")
Expand Down Expand Up @@ -620,6 +631,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
1 change: 1 addition & 0 deletions ci/ASF-Release.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ grpcurl
t/servroot
t/lib/dubbo-backend/dubbo-backend-provider/target
t/lib/dubbo-backend/dubbo-backend-interface/target
html

conf
ci/openwhisk-utilities
Expand Down
29 changes: 29 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("t/error_page/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 \@50x.html;

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

location \@50x.html {
set \$from_error_page 'true';
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 +527,18 @@ _EOC_
}
}

location \@50x.html {
set \$from_error_page 'true';
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 Expand Up @@ -625,6 +652,8 @@ $ssl_ecc_key
$test2_crt
>>> ../conf/cert/test2.key
$test2_key
>>> 50x.html
$test_50x_html
$user_apisix_yaml
_EOC_

Expand Down
38 changes: 38 additions & 0 deletions t/error_page/50x.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--
#
# 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>
Loading