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

help request: handle nginx error_pages directives #8628

Open
0prodigy opened this issue Jan 6, 2023 · 6 comments
Open

help request: handle nginx error_pages directives #8628

0prodigy opened this issue Jan 6, 2023 · 6 comments

Comments

@0prodigy
Copy link

0prodigy commented Jan 6, 2023

Description

I am trying to serve a custom HTML error page when a route is not found in Apisix. I have tried adding the error_page directive and a custom location block in the http_server_location_configuration_snippet field of the Apisix configuration file, but the custom error page is not being served. Instead, a raw JSON response with a 404 status code is being returned.

According to the documentation, the error_page directive should handle HTTP errors and serve the specified error page. However, in my case, it seems that the error_page directive is not being triggered when a route is not found in Apisix.

I have also checked the http_access_phase function in the Apisix Lua code, and it appears that a raw JSON response with a 404 status code is being returned when a route is not found. (

apisix/apisix/init.lua

Lines 522 to 525 in 37f6f81

core.log.info("not find any matched route")
return core.response.exit(404,
{error_msg = "404 Route Not Found"})
end
)

I would like to request the ability to serve a custom HTML error page when a route is not found in Apisix, either through the use of the error_page directive or through some other means.

error_page 404 /not-found;
location = /not-found {
  proxy_pass http://custom-error-pages/404.html; 
}

Environment

  • APISIX version (run apisix version): 3.0.0
  • Operating system (run uname -a): Linux (x86_64 GNU/Linux)
  • OpenResty / Nginx version (run openresty -V or nginx -V): openresty/1.21.4.1
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):
@tokers
Copy link
Contributor

tokers commented Jan 7, 2023

Agree +1. We may implement a plugin to support the custom error page.

@0prodigy
Copy link
Author

0prodigy commented Jan 8, 2023

@tokers I would like to work on this extension, can you help me to get started.

Should i create directly in plugins folder or make it 3rd-party plugin?

@tokers
Copy link
Contributor

tokers commented Jan 8, 2023

I think submit the pull request to this repo would be better.

@indrekj
Copy link
Contributor

indrekj commented Sep 21, 2023

This would be very useful. Currently when using limit-count the response body looks like this:

<html>
<head><title>429 Too Many Requests</title></head>
<body>
<center><h1>429 Too Many Requests</h1></center>
<hr><center>openresty</center>
<p><em>Powered by <a href="https://apisix.apache.org/">APISIX</a>.</em></p></body>
</html>

We need to get rid of both openresty and APISIX lines. It would be even better if we could also send a response based on the Accept header (e.g. JSON response for JSON requests).

EDIT: actually this works fine for 429 responses.

@nanamikon
Copy link
Contributor

error_page may not work if we use ngx.print or ngx.say, see openresty/lua-nginx-module#1541

For example, when route not found , core.response.exit with message will call ngx.print, in this case, error_page will not work.

    ----- /apisix/init.lua

    local route = api_ctx.matched_route
    if not route then
        -- run global rule when there is no matching route
        local global_rules = apisix_global_rules.global_rules()
        plugin.run_global_rules(api_ctx, global_rules, nil)

        core.log.info("not find any matched route")
        return core.response.exit(404,
                    {error_msg = "404 Route Not Found"})
    end

@rohitkrishna-marneni
Copy link

any updates on this? Anyone know how to implement a custom 404 page when apisix upstream throws a 404? I am trying to write a plugin, but upstream status is only available in header_filter and by that point request processing is done and I am unable to redirect again(through access,rewrite etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants