HTTP Simple Proxy Daemon
http-simple-proxy is a Node.js-based reverse proxy daemon designed to allow multiple web applications to share the same HTTP(S) ports (e.g., 80 and 443). It functions as a front-end HTTP service, providing features such as reverse proxying, URL rewriting, HTTP redirects, SSL termination, serving static files, basic HTTP authentication, and WebSocket support. It is a simpler rewrite of the 'http-master' project, intended for less complex use cases where ease of setup is prioritized over the more robust features of its predecessor. The package is currently at version 1.0.5, indicating a stable but possibly not rapidly evolving codebase. It distinguishes itself by offering an all-in-one configuration for listening ports and flexible routing based on domain names, paths, and regular expressions, enabling HTTPS for non-SSL compatible backends.
Common errors
-
Error: listen EADDRINUSE: address already in use :::80
cause Another process is already occupying the specified port (e.g., port 80 or 443).fixStop the conflicting process, or configure http-simple-proxy to listen on a different, available port. -
Proxy error: Error: SSL_CTX_use_PrivateKey_file:EE_KEY_TOO_SMALL
cause The provided SSL private key is invalid, corrupted, or too short (e.g., 512-bit key).fixGenerate a new, valid SSL private key (e.g., 2048-bit or higher) and ensure it matches the certificate. Verify the paths to the key and certificate files are correct. -
TypeError: Cannot read properties of undefined (reading 'router')
cause The configuration object passed to `init()` is malformed, specifically lacking the expected `router` property within a port definition, or `ports` itself is missing.fixDouble-check your configuration object structure. Ensure that `ports` is an object, and each port number within it contains an object that defines a `router` key. -
Requests are not being routed to the expected backend.
cause Misconfiguration in the `router` rules, such as incorrect domain names, port numbers, or overlapping/incorrect regular expression patterns.fixReview your router configuration rules carefully. Ensure domain names match exactly (or use correct wildcards), target ports are correct, and the order of rules for complex routing is as intended (most specific rules should often come before more general ones).
Warnings
- breaking http-simple-proxy is a simpler rewrite of `http-master`. Users migrating from `http-master` should expect significant differences in configuration options and available features, requiring a complete review and rewrite of proxy configurations.
- gotcha The configuration object for `init()` can become complex, especially with nested `ports` and `router` definitions. Pay close attention to the specific keys and their expected values (e.g., numbers for ports, strings for hosts, or objects for advanced rules).
- gotcha Setting up SSL (HTTPS) requires providing valid `key` and `cert` paths within the `ssl` object for the respective port. Omitting or providing incorrect paths will prevent the HTTPS server from starting or cause certificate errors for clients.
- gotcha Wildcard domain matching behavior can be subtle. `*.domain.com` matches subdomains but not `domain.com` itself, while `*?.domain.com` matches both subdomains and `domain.com`.
Install
-
npm install http-simple-proxy -
yarn add http-simple-proxy -
pnpm add http-simple-proxy
Imports
- HttpSimpleProxy
import { HttpSimpleProxy } from 'http-simple-proxy';const HttpSimpleProxy = require('http-simple-proxy'); - HttpSimpleProxy
const proxy = HttpSimpleProxy();
const proxy = new HttpSimpleProxy();
- Configuration Object
proxy.init(portsConfig, callback);
proxy.init({ ports: { ... } }, callback);
Quickstart
const HttpSimpleProxy = require('http-simple-proxy');
const httpSimpleProxy = new HttpSimpleProxy();
const config = {
ports: {
80: {
router: {
// Proxy requests for 'domain1.com' to port 3333
"domain1.com": 3333,
// Proxy requests for 'www.domain1.com' to port 3334
"www.domain1.com": 3334,
// Proxy all other requests on port 80 to a default backend on port 4080
"*": 4080
}
},
443: {
router: {
// Example for HTTPS, assuming SSL certificates are configured separately
"secure.domain.com": 5000
},
ssl: {
key: process.env.SSL_KEY_PATH ?? '', // Path to your SSL private key file
cert: process.env.SSL_CERT_PATH ?? '' // Path to your SSL certificate file
}
}
}
};
httpSimpleProxy.init(config, function(err) {
if (err) {
console.error('Proxy initialization error:', err);
} else {
console.info('http-simple-proxy started successfully');
console.info('Listening on ports 80 and 443');
}
});