A configurable-on-the-fly HTTP Proxy
npm install configurable-http-proxy





configurable-http-proxy (CHP) provides you with a way to update and manage
a proxy table using a command line interface or REST API.
It is a simple wrapper around [node-http-proxy][]. node-http-proxy is an HTTP
programmable proxying library that supports websockets and is suitable for
implementing components such as reverse proxies and load balancers. By
wrapping node-http-proxy, configurable-http-proxy extends this
functionality to [JupyterHub] deployments.
- Install
- Usage
- Starting the proxy
- Setting a default target
- Command-line options
- Using the REST API
- REST API Basics
- Authenticating via passing a token
- Getting the routing table
- Adding new routes
- Deleting routes
- Custom error pages
- Setting the path for custom error pages
- Setting a target for custom error handling
- Host-based routing
- Troubleshooting
Prerequisite: Node.js ≥ 18
If you're installing configurable-http-proxy in Linux, you can follow the instruction of nodesource to install arbitrary version of Node.js.
To install the configurable-http-proxy package globally
using npm:
``bash`
npm install -g configurable-http-proxy
To install from the source code found in this GitHub repo:
`bash`
git clone https://github.com/jupyterhub/configurable-http-proxy
cd configurable-http-proxy
npm install # Use 'npm install -g' for global install
[Return to top][]
The configurable proxy runs two HTTP(S) servers:
- The public-facing interface to your application (controlled by --ip,--port
)--api-ip
- listens on all interfaces by default.
- The inward-facing REST API (, --api-port)CONFIGPROXY_AUTH_TOKEN
- listens on localhost by default
- The REST API uses token authorization, where the token is set in the
environment variable.

[Return to top][]
`bash`
configurable-http-proxy [options]
where [options] are the command-line options described below.
[Return to top][]
The default target is used when a client has requested a URL for which
there is no routing target found in the proxy table. To set a
default target, pass the command line option,
--default-target, when starting the configurable proxy:
`bash`
configurable-http-proxy --default-target=proto://host[:port]
For example:
`bash`
configurable-http-proxy --default-target=http://localhost:8888
[Return to top][]
`
Usage: configurable-http-proxy [options]
Options:
-V, --version output the version number
--ip
--port
--ssl-key
--ssl-cert
--ssl-ca
--ssl-request-cert Request SSL certs to authenticate clients
--ssl-reject-unauthorized Reject unauthorized SSL connections (only meaningful if --ssl-request-cert is given)
--ssl-protocol
--ssl-ciphers -separated ssl cipher list. Default excludes RC4`
--ssl-allow-rc4 Allow RC4 cipher for SSL (disabled by default)
--ssl-dhparam
--api-ip
--api-port
--api-ssl-key
--api-ssl-cert
--api-ssl-ca
--api-ssl-request-cert Request SSL certs to authenticate clients for API requests
--api-ssl-reject-unauthorized Reject unauthorized SSL connections (only meaningful if --api-ssl-request-cert is given)
--client-ssl-key
--client-ssl-cert
--client-ssl-ca
--client-ssl-request-cert Request SSL certs to authenticate clients for API requests
--client-ssl-reject-unauthorized Reject unauthorized SSL connections (only meaningful if --client-ssl-request-cert is given)
--default-target
--error-target
--error-path
--redirect-port
--redirect-to
--pid-file
--no-x-forward Don't add 'X-forward-' headers to proxied requests
--no-prepend-path Avoid prepending target paths to proxied requests
--no-include-prefix Don't include the routing prefix in proxied requests
--auto-rewrite Rewrite the Location header host/port in redirect responses
--change-origin Changes the origin of the host header to the target URL
--protocol-rewrite
--custom-header
--insecure Disable SSL cert verification
--host-routing Use host routing (host as first level of path)
--metrics-ip
--metrics-port
--log-level
--timeout
--proxy-timeout
--storage-backend
-h, --help display help for command
[Return to top][]
The configurable-http-proxy REST API is documented and available as:
- a nicely rendered, interactive version at the
[petstore swagger site][]
- a [swagger specification file][] in this repo
[Return to top][]
API Root
| HTTP method | Endpoint | Function |
| ----------- | -------- | -------- |
| GET | /api/ | API Root |
Routes
| HTTP method | Endpoint | Function |
| ----------- | ------------------------ | ----------------------------------- |
| GET | /api/routes | [Get all routes in routing table][] |
| POST | /api/routes/{route_spec} | [Add a new route][] |
| DELETE | /api/routes/{route_spec} | [Remove the given route][] |
[Return to top][]
The REST API is authenticated via passing a token in the Authorization/api/routes
header. The API is served under the base URL.
For example, this curl command entered in the terminal"Authorization: token $CONFIGPROXY_AUTH_TOKEN"
passes this header forhttp://localhost:8001/api/routes
authentication and retrieves the current routing table from this endpoint,:
`bash`
curl -H "Authorization: token $CONFIGPROXY_AUTH_TOKEN" http://localhost:8001/api/routes
[Return to top][]
Request:
GET /api/routes[?inactive_since=ISO8601-timestamp]
Parameters:
inactive_since: If the inactive_since URLlast_activity
parameter is given as an ISO8601
timestamp, only routes whose is earlier than the timestamplast_activity
will be returned. The timestamp is updated whenever the proxy
passes data to or from the proxy target.
Response:
_Status code_
status: 200 OK
_Response body_
A JSON dictionary of the current routing table. This JSON
dictionary _excludes_ the default route.
Behavior:
The current routing table is returned to the user if the request is
successful.
[Return to top][]
POST requests create new routes. The body of the request should be a JSON
dictionary with at least one key: target, the target host to be proxied.
Request:
POST /api/routes/[:path]
Required input:
target: The host URL
Example request body:
`json`
{
"target": "http://localhost:8002"
}
Response:
status: 201 Created
Behavior:
After adding the new route, any request to /path/prefix on the proxy'starget
public interface will be proxied to .
[Return to top][]
Request:
DELETE /api/routes/[:path]
Response:
status: 204 No Content
Behavior:
Removes a route from the proxy's routing table.
[Return to top][]
Custom error pages can be provided when the proxy encounters an error and has
no proxy target to handle a request. There are two typical errors that CHP may
hit, along with their status code:
- 404 error: Returned when a client has requested a URL for which there is no
routing target. This error can be prevented by setting a
[default target][] before starting the configurable-http-proxy.
- 503 error: Returned when a route exists, but the upstream server isn't
responding. This is more common, and can be due to any number of reasons,
including the target service having died, not finished starting, or network
instability.
[Return to top][]
When starting the CHP, specify an error path --error-path /usr/share/chp-errors
to the location of the error page:
`bash`
configurable-http-proxy --error-path /usr/share/chp-errors
When a proxy error occurs, CHP will look in the following location for a
custom html error page to serve:
/usr/share/chp-errors/{CODE}.html
where {CODE} is a status code number for an html page to serve. If there is/usr/share/chp-errors/503.html
a 503 error, CHP will look for a custom error page in this location.
If no custom error html file exists for the error code, CHP will use the default
error.html. If you specify an error path, make sure you also createerror.html
a default file.
[Return to top][]
You can specify a target URL to use when errors occur by setting
--error-target {URL} when starting the CHP.
If, for example, CHP starts with --error-target http://localhost:1234,error-target
and the proxy encounters an error, the proxy will make a GET request to
the server. The GET request will be sent to the error-targethttp://localhost:1234
server URL, , appending the status code/{CODE}, and passing the failing request's URL escaped in a URL parameter:
GET /404?url=%2Fescaped%2Fpath
[Return to top][]
If the CHP is started with the --host-routing option, the proxy will
use the hostname of the incoming request to select a target.
When using host-based routes, the API uses the target in the same way as if
the hostname were the first part of the URL path, e.g.:
`python`
{
"/example.com": "https://localhost:1234",
"/otherdomain.biz": "http://10.0.1.4:5555",
}
[Return to top][]
Q: My proxy is not starting. What could be happening?
- If this occurs on Ubuntu/Debian, check that the you are using a recent
version of node. Some versions of Ubuntu/Debian come with a version of node
that is very old, and it is necessary to update node to a recent or LTS
version.
[Return to top][]
[node-http-proxy]: https://github.com/nodejitsu/node-http-proxy
[jupyterhub]: https://github.com/jupyterhub/jupyterhub
[petstore swagger site]: http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyterhub/configurable-http-proxy/HEAD/doc/rest-api.yml#/default
[swagger specification file]: https://github.com/jupyterhub/configurable-http-proxy/blob/HEAD/doc/rest-api.yml
[get all routes in routing table]: #getting-the-routing-table
[add a new route]: #adding-new-routes
[remove the given route]: #deleting-routes
[default target`]: #setting-a-default-target
[return to top]: #table-of-contents