jupyterhub / configurable-http-proxy

node-http-proxy plus a REST API
BSD 3-Clause "New" or "Revised" License
239 stars 128 forks source link

Routing baseurl problem #461

Closed ssb-jnk closed 1 year ago

ssb-jnk commented 1 year ago

Bug description

I have added /api to http://localhost:8001/api/routes and given the target http://172.17.0.2:8080, however when accessing it at http://localhost:8000/api i don't get expected result. However if i add '/' empty path to http://localhost:8001/api/routes and give it the same route then it works.

Expected behaviour

I expect /api to give me the JSON data provided by http://172.17.0.2:8080

Example:

{ "/api": { "target": "http://172.17.0.2:3000", "last_activity": "2023-02-05T17:07:49.205Z" }, }

should give "up'-- > curl http://172.17.0.2:8000/api/health

However if i add following and curl http://172.17.0.2:8000/health i get expected result:

{ "/": { "target": "http://172.17.0.2:3000", "last_activity": "2023-02-05T17:07:49.205Z" }, }

welcome[bot] commented 1 year ago

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively. welcome You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! :wave:
Welcome to the Jupyter community! :tada:

minrk commented 1 year ago

Can you provide a more complete example that shows the problem, and include your node and configurable-http-proxy version?

When I do:

configurable-http-proxy

curl -X POST http://localhost:8001/api/routes/api -d '{"target": "http://127.0.0.1:8888"}'
curl http://localhost:8000/api

I get the request proxied to 127.0.0.1:8888/api, as requested. There's no difference in the proxy logs for /api/ and /api:

$ configurable-http-proxy
08:55:12.097 [ConfigProxy] warn: REST API is not authenticated.
08:55:12.107 [ConfigProxy] info: Proxying http://*:8000 to (no default)
08:55:12.107 [ConfigProxy] info: Proxy API at http://localhost:8001/api/routes
# curl -X POST http://localhost:8001/api/routes/api -d '{"target": "http://127.0.0.1:8888"}'
08:56:20.888 [ConfigProxy] info: Adding route /api -> http://127.0.0.1:8888
08:56:20.889 [ConfigProxy] info: Route added /api -> http://127.0.0.1:8888
08:56:20.893 [ConfigProxy] info: 201 POST /api/routes/api
# curl -X POST http://localhost:8001/api/routes/api/ -d '{"target": "http://127.0.0.1:8889"}'
09:00:04.466 [ConfigProxy] info: Adding route /api -> http://127.0.0.1:8889
09:00:04.466 [ConfigProxy] info: Route added /api -> http://127.0.0.1:8889
09:00:04.467 [ConfigProxy] info: 201 POST /api/routes/api/
ssb-jnk commented 1 year ago

Thanks for replying. Sure thing!

I use the latest version of chp( not configured anything ): https://hub.docker.com/r/jupyterhub/configurable-http-proxy I use the latest version (dummy api for testing): cloudkats/hello-world-rest

POST http://localhost:8001/api/routes/api
{
  "target": "http://172.17.0.3:3000"
}
GET http://localhost:8001/api/routes
{
    "/api": {
        "target": "http://172.17.0.3:3000",
        "last_activity": "2023-02-06T08:27:12.213Z"
    }
}
GET http://localhost:8000/api/version

Error: No default engine was specified and no extension was provided.<br> &nbsp; &nbsp;at new View (/usr/src/app/node_modules/express/lib/view.js:61:11)<br> &nbsp; &nbsp;at Function.render (/usr/src/app/node_modules/express/lib/application.js:587:12)<br> &nbsp; &nbsp;at ServerResponse.render (/usr/src/app/node_modules/express/lib/response.js:1039:7)<br> &nbsp; &nbsp;at /usr/src/app/lib/app.js:61:19<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/usr/src/app/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at trim_prefix (/usr/src/app/node_modules/express/lib/router/index.js:328:13)<br> &nbsp; &nbsp;at /usr/src/app/node_modules/express/lib/router/index.js:286:9<br> &nbsp; &nbsp;at Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:346:12)<br> &nbsp; &nbsp;at next (/usr/src/app/node_modules/express/lib/router/index.js:280:10)<br> &nbsp; &nbsp;at /usr/src/app/lib/app.js:14:3

It appears that GET http://localhost:8000/api/version routes to http://172.17.0.3:3000/api/version and not http://172.17.0.3:3000/version. The reason i believe this is because if i add following route, then it works by accessing http://localhost:8000/version

{
    "/": {
        "target": "http://172.17.0.3:3000",
        "last_activity": "2023-02-06T08:27:12.213Z"
    }
}
minrk commented 1 year ago

It appears that GET http://localhost:8000/api/version routes to http://172.17.0.3:3000/api/version and not http://172.17.0.3:3000/version.

Yes, that is the intended behavior. If you don't want to preserve URL prefixes in the proxied request, I think you want to add --no-include-prefix, which will strip the prefix from the URL before proxying.

ssb-jnk commented 1 year ago

Aha, so this is a typical PEBKAC issue, thanks for helping me out. Appreciate it!