simple HTTP | HTTPS | WS | WSS reverse proxy in node.js. currently supports:
- reverse proxy using incoming request's
x-forwarded-host
orhost
header to pre-configured origin servers. see Host and X-Forwarded-Host - proxy incoming
http:
orhttps:
request to originhttp:
orhttps:
servers - proxy incoming
ws:
orwss:
request to originws:
orwss:
servers - supports graceful shutdowns
- supports round-robin origin server selection
- supports force upgrade
http:
tohttps:
andws:
towss:
- for global installation
npm i fimiproxy -g
- for local installation
npm i fimiproxy
- for local dev-dependency installation
npm i fimiproxy -D
replace npm
with yarn
or any other package manager of choice.
{
"exposeHttpProxy": false,
"httpPort": "",
"exposeHttpsProxy": false,
"httpsPort": "",
"exposeWsProxyForHttp": false,
"exposeWsProxyForHttps": false,
"httpsPublicKeyFilepath": "",
"httpsPrivateKeyFilepath": "",
"httpsPublicKey": "",
"httpsPrivateKey": "",
"routes": [
{
"origin": [
{
"originHost": "",
"originPort": "",
"originProtocol": "http:"
},
{
"originHost": "",
"originPort": "",
"originProtocol": "ws:"
}
],
"incomingHostAndPort": "",
"forceUpgradeHttpToHttps": false,
"forceUpgradeWsToWss": false,
"usePermanentRedirect": false,
"redirectHost": ""
}
],
"forceUpgradeHttpToHttps": false,
"forceUpgradeWsToWss": false,
"usePermanentRedirect": false,
"redirectHost": ""
}
exposeHttpProxy
— set totrue
to expose an HTTP server, requireshttpPort
to be set iftrue
exposeHttpsProxy
— set totrue
to expose an HTTPS server, requireshttpsPort
,httpsPublicKey
ORhttpsPublicKeyFilepath
,httpsPrivateKey
ORhttpsPrivateKeyFilepath
to be set iftrue
exposeWsProxyForHttp
— set totrue
to expose a WebSocket server for HTTP requests, requireshttpPort
andexposeHttpProxy
to be set iftrue
exposeWsProxyForHttps
— set totrue
to expose a WebSocket server for HTTPS requests, requireshttpsPort
andexposeHttpsProxy
to be set iftrue
httpPort
— port HTTP server should listen on, whenexposeHttpProxy
istrue
httpsPort
— port HTTPS server should listen on, whenexposeHttpsProxy
istrue
httpsPublicKeyFilepath
— filepath to TLS certificate (public key) used with HTTPS serverhttpsPrivateKeyFilepath
— filepath to TLS private key used with HTTPS serverhttpsPublicKey
— TLS certificate (public key) string used with HTTPS server. takes precedence overhttpsPublicKeyFilepath
httpsPrivateKey
— TLS private key string used with HTTPS server. takes precedence overhttpsPrivateKeyFilepath
routes
— array of incoming host to origin protocol, host, and portorigin
— array of origin server host, port, and protocoloriginHost
— origin host or IPoriginPort
— origin portoriginProtocol
— origin protocol. one ofhttp:
orhttps:
orws:
orwss:
. don't forget the:
at the end
incomingHostAndPort
— incominghost:port
to proxy to origin server. picked from HTTPhost
header fieldforceUpgradeHttpToHttps
— set totrue
to force upgradehttp:
request tohttps:
requestforceUpgradeWsToWss
— set totrue
to force upgradews:
request towss:
requestusePermanentRedirect
— set totrue
to use permanent redirect. The proxy server will return a308
redirect response to the client instead of the default307
temporary redirect response.redirectHost
— host to redirect to, e.g. when upgrading to HTTPS or WSS, or if the incoming host is no longer supported and all requests to it should be redirected somewhere else. if not set, the proxy server will redirect to the incomingx-forwarded-host
orhost
header field.overrideHost
— host origin requesthost
andx-forwarded-host
header fields are set to.
forceUpgradeHttpToHttps
— set totrue
to force upgradehttp:
request tohttps:
requestforceUpgradeWsToWss
— set totrue
to force upgradews:
request towss:
requestusePermanentRedirect
— set totrue
to use permanent redirect. The proxy server will return a308
redirect response to the client instead of the default307
temporary redirect response.redirectHost
— host to redirect to, e.g. when upgrading to HTTPS or WSS, or if the incoming host is no longer supported and all requests to it should be redirected somewhere else. if not set, the proxy server will redirect to the incomingx-forwarded-host
orhost
header field.
- if installed globally, run
fimiproxy ./path/to/config.json
- if installed locally, run
npm exec fimiproxy ./path/to/config.json
- for one-time run, run
npx -y fimiproxy ./path/to/config.json
import fimiproxy from "fimiproxy"
// start fimiproxy
await fimiproxy.startFimiproxyUsingConfig({
/** config */ {
exposeHttpProxy: false,
exposeHttpsProxy: false,
httpPort: "80",
httpsPort: "443",
routes: [{
origin: [{
originHost: "localhost",
originPort: "0000",
originProtocol: "https:",
}],
incomingHostAndPort: "www.fimidara.com:80",
}],
httpsPublicKey: "",
httpsPrivateKey: "",
httpsPublicKeyFilepath: "",
httpsPrivateKeyFilepath: "",
},
/** shouldHandleGracefulShutdown */ true,
/** exitProcessOnShutdown */ true,
});
// end fimiproxy
await fimiproxy.endFimiproxy(/** exitProcessOnShutdown */ true);
startFimiproxyUsingConfig
— start fimiproxy using configconfig: FimiproxyRuntimeConfig
— see configuration aboveshouldHandleGracefulShutdown
— defaults totrue
. iftrue
, will listen forSIGINT
andSIGTERM
, and attempt to gracefully shut down the proxy serverexitProcessOnShutdown
— defaults totrue
. ifshouldHandleGracefulShutdown
istrue
, will callprocess.exit()
after graceful shutdown. your process may not shut down afterSIGINT
andSIGTERM
if nottrue
. currently untested behaviour (if process will shutdown or not) when set tofalse
andshouldHandleGracefulShutdown
istrue
startFimiproxyUsingConfigFile
— start fimiproxy using config read from filepathfilepath: string
— file at filepath should be a json file, see configuration section above
startFimiproxyUsingProcessArgs
— start fimiproxy using filepath picked fromprocess.argv[2]
see https://nodejs.org/docs/latest/api/process.html#processargv. example,node your-script.js ./path/to/config.json
endFimiproxy
— gracefully end fimiproxyexitProcess
— defaults totrue
. callsprocess.exit()
iftrue
- cannot sustain multiple start calls, because current state is managed using a module-global variable. we'll eventually transition to a class-based encapsulation system, so stick around (if you're versed in Typescript, you can contribute to this effort). multiple start calls will either lead to existing servers being garbage collected or memory leak, i haven't tested it. so call
endFimiproxy
before making another start call. start calls are calls tostartFimiproxyUsingConfig
,startFimiproxyUsingConfigFile
, orstartFimiproxyUsingProcessArgs