apisix icon indicating copy to clipboard operation
apisix copied to clipboard

help request: after apisix enables the ext-plugin-post-resp plugin, the domain name is rewritten abnormally

Open forget99 opened this issue 2 years ago • 12 comments

Description

在apisix路由中配置了一个上游服务,上游服务只支持域名访问,不能通过ip + port的方式访问,在路由中设置了域名重写,服务可以正常调用,但是在路由上加上ext-plugin-post-resp后置插件,调用服务404.

` "ext-plugin-post-resp": { "allow_degradation": true, "conf": [ { "name": "AuthFilter", "value": "authFilter" } ], "disable": false }

"proxy-rewrite": { "host": "test.com", "regex_uri": [ "^/test-api(/|$)(.*)", "/$2" ], "use_real_request_uri_unsafe": false }`

查看apisix error.log日志文件打印信息 common_phase(): ext-plugin-post-resp exits with http status code 404

查看apisix access.log 日志文件打印信息 HTTP/1.1" 404 157 0.107 "-" "PostmanRuntime/7.29.2" - - -

请各位技术老师帮忙看下!

Environment

  • APISIX version (run apisix version):
  • Operating system (run uname -a):
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • 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):

forget99 avatar Jun 21 '23 08:06 forget99

@forget99 Can you paste the full access log where path is also visible? Can you also share the business logic of your AuthFilter plugin that is running ?

Revolyssup avatar Jun 21 '23 10:06 Revolyssup

@forget99 Can you paste the full access log where path is also visible? Can you also share the business logic of your AuthFilter plugin that is running ?

当在配置路由时,只配置了前置插件(ext-plugin-pre-req),并且配置了域名重写,此时通过apisix调用上游服务(https服务,只支持域名访问,不支持ip + post)是调用成功的。 { "uri": "/test-api/*", "name": "测试api", "desc": "测试api", "methods": ["GET", "POST"], "plugins": { "ext-plugin-pre-req": { "allow_degradation": true, "conf": [{ "name": "AuthFilter", "value": "authFilter" }], "disable": false }, "proxy-rewrite": { "host": "test.com", "regex_uri": ["^/test-api(/|$)(.*)", "/$2"], "use_real_request_uri_unsafe": false } }, "service_id": "1671453424892182530", "labels": { "API_VERSION": "v1" }, "status": 1 }

此时调用的 access.log 日志信息如下: ` 10.19.36.132 - - [25/Jun/2023:09:29:42 +0800] 192.17.101.106:9080 "POST /test-api/vehicle/info/queryVerifyByVehicleNo HTTP/1.1" 200 111 0.056 "-" "PostmanRuntime/7.29.2" 39.105.180.130:80 200 0.048 "http://test.com.com/vehicle/info/queryVerifyByVehicleNo"

10.19.36.132 - - [25/Jun/2023:09:29:43 +0800] 192.17.101.106:9080 "POST /test-api/vehicle/info/queryVerifyByVehicleNo HTTP/1.1" 200 111 0.062 "-" "PostmanRuntime/7.29.2" 39.105.180.130:80 200 0.052 "http://test.com.com/vehicle/info/queryVerifyByVehicleNo"

10.19.36.132 - - [25/Jun/2023:09:29:43 +0800] 192.17.101.106:9080 "POST /test-api/vehicle/info/queryVerifyByVehicleNo HTTP/1.1" 200 111 0.053 "-" "PostmanRuntime/7.29.2" 39.105.180.130:80 200 0.048 "http://test.com.com//vehicle/info/queryVerifyByVehicleNo" `

当在配置路由时,同时配置了前置插件(ext-plugin-pre-req),后置插件(ext-plugin-post-resp),同时配置了域名重写,此时通过apisix调用上游服务(https服务,只支持域名访问,不支持ip+port)调用不成功,原因可能是后置插件在调用上游服务时,采用的是ip + port调用,上游服务的nginx给拦截了。

{ "uri": "/test-api/*", "name": "测试api", "desc": "测试api", "methods": ["GET", "POST"], "plugins": { "ext-plugin-post-resp": { "allow_degradation": false, "conf": [{ "name": "AuthFilter", "value": "authFilter" }], "disable": false }, "ext-plugin-pre-req": { "allow_degradation": true, "conf": [{ "name": "AuthFilter", "value": "authFilter" }], "disable": false }, "proxy-rewrite": { "host": "test.com", "regex_uri": ["^/test-api(/|$)(.*)", "/$2"], "use_real_request_uri_unsafe": false } }, "service_id": "1671453424892182530", "labels": { "API_VERSION": "v1" }, "status": 1 }

此时调用的 access.log 日志信息如下,从日志中可以看出上游服务接口响应404,调用失败: ` 10.19.36.132 - - [25/Jun/2023:09:54:40 +0800] 192.17.101.106:9080 "POST /test-api/vehicle/info/queryVerifyByVehicleNo HTTP/1.1" 404 157 0.098 "-" "PostmanRuntime/7.29.2" - - - "http://test.com.com//vehicle/info/queryVerifyByVehicleNo"

10.19.36.132 - - [25/Jun/2023:09:54:40 +0800] 192.17.101.106:9080 "POST /test-api/vehicle/info/queryVerifyByVehicleNo HTTP/1.1" 404 157 0.076 "-" "PostmanRuntime/7.29.2" - - - "http://test.com.com//vehicle/info/queryVerifyByVehicleNo"

10.19.36.132 - - [25/Jun/2023:09:54:41 +0800] 192.17.101.106:9080 "POST /test-api/vehicle/info/queryVerifyByVehicleNo HTTP/1.1" 404 157 0.073 "-" "PostmanRuntime/7.29.2" - - - "http://test.com.com//vehicle/info/queryVerifyByVehicleNo" `

AuthFilter过滤器逻辑如下: ` @Component public class AuthFilter implements PluginFilter {

private final Logger logger = LoggerFactory.getLogger(AuthFilter.class);

@Override
public String name() {
    return "AuthFilter";
}

@Override
public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
    logger.info("【进入前置拦截过滤器】");
    // 在前置插件中获取接口的请求相关信息
    chain.filter(request, response);
}

@Override
public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) {
    logger.info("【进入后置拦截过滤器】");
    // 在后置插件中获取到上游服务的响应结果,但是这里在调用上游服务时应该把域名调用转为了ip + port调用,上游服务的nginx给拦截了
    chain.postFilter(request, response);
}

@Override
public List<String> requiredVars() {
    List<String> vars = new ArrayList<>();
    vars.add("remote_addr");
    vars.add("server_port");
    vars.add("request_id");
    return vars;
}

@Override
public Boolean requiredBody() {
    return true;
}

@Override
public Boolean requiredRespBody() {
    return true;
}

} `

请技术老师帮忙看下,感谢!!!

forget99 avatar Jun 25 '23 02:06 forget99

@Revolyssup can you help me see? This problem has been bothering me for a long time,Thank you!

forget99 avatar Jun 25 '23 02:06 forget99

Is the code pasted above as it is in the runner or some part of it is replaced with the comment? Because I don't see any manipulation with Response in the postResponse method but according to error logs the plugin runner sets response status code as 404 @forget99

Revolyssup avatar Jun 25 '23 17:06 Revolyssup

Is the code pasted above as it is in the runner or some part of it is replaced with the comment? Because I don't see any manipulation with Response in the postResponse method but according to error logs the plugin runner sets response status code as 404 @forget99

@Revolyssup in postFilter plugin runner use request.getBody(),but i get upstream service response 404。

@Override public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) { logger.info("【进入后置拦截过滤器】"); // 在后置插件中获取到上游服务的响应结果,但是这里在调用上游服务时应该把域名调用转为了ip + port调用,上游服务的nginx给拦截了 String body = request.getBody(); chain.postFilter(request, response); }

forget99 avatar Jun 27 '23 03:06 forget99

Is the code pasted above as it is in the runner or some part of it is replaced with the comment? Because I don't see any manipulation with Response in the postResponse method but according to error logs the plugin runner sets response status code as 404 @forget99

@Revolyssup When I remove ext-plugin-post-resp from the route,i get upstream service response 200。Curious and strange, it troubles me a lot。

forget99 avatar Jun 27 '23 03:06 forget99

Is the code pasted above as it is in the runner or some part of it is replaced with the comment? Because I don't see any manipulation with Response in the postResponse method but according to error logs the plugin runner sets response status code as 404 @forget99

@Revolyssup Please help me take a look, thank you

forget99 avatar Jun 27 '23 03:06 forget99

@forget99 Do you get the expected body if you print request.getBody() in postFilter? To clarify, ext-plugin- post-resp is run after a response is returned by the upstream and it doesn't call the upstream itself. I still don't see any business logic which change or tamper the response returned by upstream.

Also for debugging and isolating the problem, can you confirm that even if you remove all other plugins and leave only ext-post-resp plugin in the configuration, the error still remains?

Revolyssup avatar Jun 27 '23 05:06 Revolyssup

@forget99 Do you get the expected body if you print request.getBody() in postFilter? To clarify, ext-plugin- post-resp is run after a response is returned by the upstream and it doesn't call the upstream itself. I still don't see any business logic which change or tamper the response returned by upstream. Also for debugging and isolating the problem, can you confirm that even if you remove all other plugins and leave only ext-post-resp plugin in the configuration, the error still remains?

@Revolyssup I print request.getBody() in PostFilter,Not achieving the expected results,I got this result: `

404 Not Found # 404 Not Found nginx `

when i remove all other plugins and leave only ext-post-resp plugin in the configuration,The error still exists,but when i remove all other plugins,Rewritten domain name,Call successful。 { "uri": "/test-api/*", "name": "测试api", "desc": "测试api", "methods": [ "GET", "POST" ], "plugins": { "proxy-rewrite": { "host": "test.com", "regex_uri": [ "^/test-api/(/|$)(.*)", "/$2" ], "use_real_request_uri_unsafe": false } }, "service_id": "1673602146597138445", "labels": { "API_VERSION": "v1" }, "status": 1 }

forget99 avatar Jun 27 '23 08:06 forget99

@forget99 Do you get the expected body if you print request.getBody() in postFilter? To clarify, ext-plugin- post-resp is run after a response is returned by the upstream and it doesn't call the upstream itself. I still don't see any business logic which change or tamper the response returned by upstream.

Also for debugging and isolating the problem, can you confirm that even if you remove all other plugins and leave only ext-post-resp plugin in the configuration, the error still remains?

I think the ext-plugin- post-resp plugin affected the results

forget99 avatar Jun 27 '23 08:06 forget99

This issue has been marked as stale due to 350 days of inactivity. It will be closed in 2 weeks if no further activity occurs. If this issue is still relevant, please simply write any comment. Even if closed, you can still revive the issue at any time or discuss it on the [email protected] list. Thank you for your contributions.

github-actions[bot] avatar Jun 11 '24 10:06 github-actions[bot]

I have also encountered the same problem. Is there a solution? When I use IP and port number to access, I can successfully access it, but when I rewrite the domain name, it fails

a937259612 avatar Jun 15 '24 01:06 a937259612

This issue has been marked as stale due to 350 days of inactivity. It will be closed in 2 weeks if no further activity occurs. If this issue is still relevant, please simply write any comment. Even if closed, you can still revive the issue at any time or discuss it on the [email protected] list. Thank you for your contributions.

github-actions[bot] avatar May 31 '25 10:05 github-actions[bot]

This issue has been closed due to lack of activity. If you think that is incorrect, or the issue requires additional review, you can revive the issue at any time.

github-actions[bot] avatar Jun 14 '25 10:06 github-actions[bot]