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

How to rewrite or customize the default 404 response? #3192

Closed
Maples7 opened this issue Feb 2, 2018 · 9 comments
Closed

How to rewrite or customize the default 404 response? #3192

Maples7 opened this issue Feb 2, 2018 · 9 comments
Labels
pending author feedback Waiting for the issue author to get back to a maintainer with findings, more details, etc...

Comments

@Maples7
Copy link

Maples7 commented Feb 2, 2018

When a user requests a non-existent API, kong will return 404 with

{
    "message": "no API found with those values"
} 

Is there any way to rewrite the response body or customize 404 response? 🤔

@dustinschultz
Copy link

dustinschultz commented Feb 5, 2018

Update:

https://getkong.org/docs/0.12.x/proxy/#configuring-a-fallback-api

You can use a combination of a root-fallback and the request termination plugin:

Fallback API

curl -X POST \
  http://localhost:8001/apis \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "root-fallback",
    "upstream_url": "http://_:-1",
    "uris": ["/"]
}'

Termination Plugin responds with 404 and html w/ javascript to redirect.

curl -X POST \
  http://localhost:8001/apis/root-fallback/plugins \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "request-termination",
    "config.status_code": "404",
    "config.body": "<html><head><script>window.location.replace('\''http://stackoverflow.com'\'');</script></head><body></body></html>",
    "config.content_type": "text/html; charset=utf-8"
}'

@bungle
Copy link
Member

bungle commented Feb 14, 2018

@Maples7, please close this issue if @dustinschultz's answer gave you an inspiration. If not, please let us know the issues of that aproach, thank you!

@kikito kikito added pending author feedback Waiting for the issue author to get back to a maintainer with findings, more details, etc... and removed question labels Feb 15, 2018
@Maples7
Copy link
Author

Maples7 commented Mar 5, 2018

@dustinschultz Thank you very much for your solution. But after I follow your instruction, API doesn't return 404 and the request hits my custom plugins. I feel a little confused.

Sorry for reply so late due to Lunar New Year.

@Maples7
Copy link
Author

Maples7 commented Mar 5, 2018

And I'd like to know that "_" and "-1" are wildcards?

@Maples7
Copy link
Author

Maples7 commented Mar 6, 2018

I have figured out why the requests would hit my custom plugins which have higher priority. So the question goes to how can I make plugin request-termination get higher priority than my custom plugins.

@Maples7
Copy link
Author

Maples7 commented Mar 6, 2018

I've read the source code of plugin request-termination and its priority is 2, that's not enough for my need.

@Maples7
Copy link
Author

Maples7 commented Mar 6, 2018

I solve the problem finally by adding code logic of 404 response in my own custom plugin and it works well. Thank U all guys 4 providing this method! :D

@Maples7 Maples7 closed this as completed Mar 6, 2018
@tzssangglass
Copy link
Member

I solve the problem finally by adding code logic of 404 response in my own custom plugin and it works well. Thank U all guys 4 providing this method! :D
hi,请问方便分享一下你的自定义插件吗,我在学习这个

@zffocussss
Copy link

this plugin all the request 404 no matter which kind of response code is sent by backend server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending author feedback Waiting for the issue author to get back to a maintainer with findings, more details, etc...
Projects
None yet
Development

No branches or pull requests

6 participants