tyk icon indicating copy to clipboard operation
tyk copied to clipboard

How to define proxy.listen_path as exact path?

Open vm-001 opened this issue 1 year ago • 4 comments

I have an API definition as below:

{
  "name": "/api/v1/users/{id}",
  "api_id": "1",
  "org_id": "test",
  "use_keyless": true,
  "version_data": {
    "not_versioned": true,
    "versions": {
      "Default": {
        "name": "Default",
        "use_extended_paths": true
      }
    }
  },
  "proxy": {
    "listen_path": "/api/v1/users/{id}",
    "target_url": "http://host.docker.internal:8000/",
    "strip_listen_path": true
  },
  "active": true
}
  • request to the path /api/v1/users/1 can be proxied to the target_url, which is expected.
  • request to the path /api/v1/users/1/profile now be proxied to the target_url as well, which is not expected.

It seems like the proxy.listen_path behaves like a prefix matching, I would like to know why and I'm wondering if there is a way to define a proxy.listen_path as a exact path.

vm-001 avatar Apr 02 '24 08:04 vm-001

Hi @vm-001,

Have you tried enabling "strict routes" in the Gateway config? This should enable you to get the behaviour you require. https://tyk.io/docs/tyk-oss-gateway/configuration/#http_server_optionsenable_strict_routes

Please let me know if this works for you.

andyo-tyk avatar Apr 02 '24 10:04 andyo-tyk

@andyo-tyk Hey. The configuration http_server_options.enable_strict_routes does not work for this case. Request to /api/v1/users/1/profile still be proxied to the target_url.

vm-001 avatar Apr 02 '24 15:04 vm-001

Hi @vm-001, Internally, Tyk treats the path as a regex - can you pleae try putting a $ on the end of your listen path?

andyo-tyk avatar Apr 04 '24 13:04 andyo-tyk

@andyo-tyk The $ was treated as a literal character.

{
  "name": "test",
  "api_id": "1",
  "org_id": "test",
  "use_keyless": true,
  "version_data": {
    "not_versioned": true,
    "versions": {
      "Default": {
        "name": "Default",
        "use_extended_paths": true
      }
    }
  },
  "proxy": {
    "listen_path": "/api$",
    "target_url": "http://host.docker.internal:8888/",
    "strip_listen_path": false
  },
  "active": true
}
$ curl http://localhost:8080/api
Not Found
$ curl http://localhost:8080/api$
proxied to the upstream

vm-001 avatar Apr 04 '24 14:04 vm-001