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

支持nginx的error_page功能 #1612

Open
haifzhu opened this issue Dec 20, 2024 · 3 comments
Open

支持nginx的error_page功能 #1612

haifzhu opened this issue Dec 20, 2024 · 3 comments
Labels

Comments

@haifzhu
Copy link
Collaborator

haifzhu commented Dec 20, 2024

Why do you need it?

默认在一些常见的异常页面,比如404, 503等,为了给用户一个好的信息反馈,会单独定制一些页面。nginx支持这块功能使用的配置指令是error_page, 具体用法参见: https://nginx.org/en/docs/http/ngx_http_core_module.html#error_page

How could it be?

具体功能类似nginx的error_page功能,可以配置什么返回码对应哪些页面,这个页面可能是存储在oss,也可能是在某个服务上的。
比如如果页面请求返回404,整体返回404的一个提示页面。

Other related information

Add any other context or screenshots about the feature request here.

@w7team
Copy link

w7team commented Dec 20, 2024

目前client请求外部地址需要经过mcpbridges,插件里如果集成外部对象存储的地址,配置起来还怪麻烦的

@johnlanni
Copy link
Collaborator

higress 目前扩展了 envoy 的 custom-response filter 的能力,可以实现类似的效果。

例如这个是对“abc”这条路由,配置一个fallback,当触发4xx,5xx时,加上一个 header: x-higress-fallback-from: abc 然后重新路由。

目标的fallback路由上,可以增加一个 header 精确匹配x-higress-fallback-from = abc,这样就实现了 error_page 的能力。

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: custom-response-route-abc
  namespace: higress-system
spec:
  configPatches:
  - applyTo: HTTP_ROUTE
    match:
      context: GATEWAY
      routeConfiguration:
        vhost:
          route:
            name: "abc"
    patch:
      operation: MERGE
      value:
        typed_per_filter_config:
          envoy.filters.http.custom_response:
            "@type": type.googleapis.com/udpa.type.v1.TypedStruct
            type_url: type.googleapis.com/envoy.extensions.filters.http.custom_response.v3.CustomResponse
            value:
              with_request_body:
                max_request_bytes: 1024000            
              custom_response_matcher:
                matcher_list:
                  matchers:
                  - predicate:
                      or_matcher:
                        predicate:
                        - single_predicate:
                            input:
                              name: "4xx_response"
                              typed_config:
                                "@type": type.googleapis.com/envoy.type.matcher.v3.HttpResponseStatusCodeClassMatchInput
                            value_match:
                              exact: "4xx"
                        - single_predicate:
                            input:
                              name: "5xx_response"
                              typed_config:
                               "@type": type.googleapis.com/envoy.type.matcher.v3.HttpResponseStatusCodeClassMatchInput
                            value_match:
                              exact: "5xx"
                    on_match:
                      action:
                        name: action
                        typed_config:
                          "@type": type.googleapis.com/udpa.type.v1.TypedStruct
                          type_url: type.googleapis.com/envoy.extensions.http.custom_response.redirect_policy.v3.RedirectPolicy
                          value:
                            max_internal_redirects: 10
                            use_original_request_uri: true
                            keep_original_response_code: false
                            use_original_request_body: true
                            only_redirect_upstream_code: false
                            request_headers_to_add:
                            - header:
                                key: "x-higress-fallback-from"
                                value: "qwen"
                              append: false
                            response_headers_to_add:
                            - header:
                                key: "x-higress-fallback-from"
                                value: "qwen"
                              append: false      

matcher_list 部分的配置,除了 4xx,5xx 也可以配置精确的状态码,具体可以参考 envoy 的文档

higress 做的扩展主要是 RedirectPolicy 这个 action 的配置,增加了几个配置项:

  1. max_internal_redirects:支持了嵌套重定向,并支持配置最大重定向次数
  2. use_original_request_uri:支持使用请求原本的 request_uri 进行重定向(通过添加不同 header 来重试到不同路由上)
  3. keep_original_response_code:重定向后支持使用重定向服务返回的装态码
  4. use_original_request_body:支持携带原始请求body
  5. only_redirect_upstream_code:用于控制是否只对后端返回的状态码做 4xx,5xx 等匹配,类似nginx的proxy_intercept_errors

@johnlanni
Copy link
Collaborator

                    on_match:
                      action:
                        name: action
                        typed_config:
                          "@type": type.googleapis.com/udpa.type.v1.TypedStruct
                          type_url: type.googleapis.com/envoy.extensions.http.custom_response.redirect_policy.v3.RedirectPolicy
                          value:
                            uri: https://www.mydomain.com/path/to/404.txt  

简单的用例,这样就可以。

可以考虑设计一个 ingress 注解,来实现完整的 nginx error_page 功能

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

No branches or pull requests

3 participants