apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
14.44k stars 2.51k forks source link

help request: Can proxy-rewite plugin support multiple path rewrite? #9258

Closed GloryAndDream closed 4 months ago

GloryAndDream commented 1 year ago

Description

apiVersion: apisix.apache.org/v2 kind: ApisixRoute metadata: name: service-route namespace: dev spec: http:

  • name: machine-service-app match: hosts:
  • api-test.xxx.com paths:
  • "/machine/*"
  • "/pay/*" plugins:
  • name: proxy-rewrite enable: true config: regex_uri: ["^/machine|pay(/|$)(.*)", "/$2"] backends:
  • serviceName: machine-service-app servicePort: 8001

I want to proxy-rewrite support multiple paths, but even I got regexp correct, it didn't work for both paths:

e.g. regexp is below:

*^/machine|pay(/|$)(.)**

can match below paths:

/machine/tests /machine /machine/ /pay/testset /pay/

However, when I deploy this configurations, it can only match paths begin with **/machine/* , not the /pay/xxx**

Is there a solution or not?

Environment

kingluo commented 1 year ago

Your regex is wrong.

Example:

curl http://127.0.0.1:9180/apisix/admin/routes/1  \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "methods": ["GET"],
    "uri": "/*",
    "plugins": {
        "proxy-rewrite": {
            "regex_uri": ["^/(machine|pay)(/.*)", "/$2"]
        }
    },
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "httpbin.org": 1
        }
    }
}'
GloryAndDream commented 1 year ago

Your regex is wrong.

Example:

curl http://127.0.0.1:9180/apisix/admin/routes/1  \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "methods": ["GET"],
    "uri": "/*",
    "plugins": {
        "proxy-rewrite": {
            "regex_uri": ["^/(machine|pay)(/.*)", "/$2"]
        }
    },
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "httpbin.org": 1
        }
    }
}'

I tried you regexp, still not working

http://api-test.xxx.com/machine/health


Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Apr 07 21:56:08 CST 2023
There was an unexpected error (type=Not Found, status=404).

http://api-test.xxx.com/machine

{"error_msg":"404 Route Not Found"}

seems only first part of path goes to controller, by the way, the API is built on springboot, the path /machine/health is configured

kingluo commented 1 year ago

What part of the uri do you want to extract? In my code, I assume you need to get the second part, that is, for /machine/health, you need /health. If that's not your case, you need to configure the target uri you need for the upstream server. In your output, the request http://api-test.xxx.com/machine/health will be changed to http://api-test.xxx.com/health, and it's obvious it's sent to the controller server, but gets 404 finally. Anyways, you need to debug your regexp to fit your need.

GloryAndDream commented 1 year ago
"^/(machine|pay)(/.*)", "/$2"

Yes, I need to extract second part, but I tried this regexp "^/machine(/.*)", "/$2" , it worked fine, the url http://api-test.xxx.com/machine/health is accessible

it seems the plugin can't handle with symbol "|" , don't know it is a bug or not

kingluo commented 1 year ago

| must be quoted with (. That's not a bug, it's regex usage issue.

What's your question? I'm really confused.

However, when I deploy this configurations, it can only match paths begin with /machine/* , not the /pay/xxx

or

http://api-test.xxx.com/machine

{"error_msg":"404 Route Not Found"}`

Anyways, the regex obeys the pcre regex standard, please change your regex to fit your need.

GloryAndDream commented 1 year ago

I need to match below urls: e.g.

http://api-test.xxx.com/machine/aaa

http://api-test.xxx.com/pay/bbb

however, I can't find the correct regexp to get what I need with plugin proxy-rewite

GloryAndDream commented 1 year ago

anyone can help me with correct regex string?

github-actions[bot] commented 5 months ago

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 dev@apisix.apache.org list. Thank you for your contributions.

github-actions[bot] commented 4 months ago

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.