Hipache is a distributed proxy designed to route high volumes of HTTP and WebSocket traffic. Hipache supports dynamic configuration via Redis, so changing the configuration and adding vhosts does not require a restart. Based on the node-http-proxy library, Hipache provides support for load balancing websockets, SSL, dead backend detection, and is clustered for failover. Let's install it:
npm install hipache -g
Let's setup a host for both vision-web
and vision-api
by editing the hosts
file:
sudo nano /private/etc/hosts
Add two new entries:
127.0.0.1 web.vision.net 127.0.0.1 api.vision.net
And then flush the cache for these changes to take effect:
dscacheutil -flushcache
In order to configure a server, we will need a configuration file for each application we want to load balance. In our case, it is vision-web
and vision-api
. Here is the configuration file for vision-api
, ./config/server.json
. Importantly, we are running vision-api
on port 8443
. We configure an SSL certificate under the HTTPS section as Hipache will terminate SSL not our Express server:
{ "server": { "accessLog": "hipache_access.log", "port": 8443, "workers": 5, "maxSockets": 100, "deadBackendTTL": 30, "address": ["127.0.0.1"], "address6": ["::1"], "https": { "port": 8443, "key": "lib/secure/key.pem", "cert": "lib/secure/cert.pem" } }, "redisHost": "127.0.0.1", "redisPort": 6379, "redisDatabase": 0 }
Let's make a change to the Express server ./lib/express/server.js
, and return a standard HTTP server when running in production; Hipache will now terminate SSL.
function Server(app){ if (process.env['NODE_ENV'] === "PRODUCTION") return http.createServer(app).listen(app.get('port')); var httpsOptions = { key: fs.readFileSync('./lib/secure/key.pem'), cert: fs.readFileSync('./lib/secure/cert.pem') }; return https.createServer(httpsOptions,app).listen(app.get('port')); }
We now add Hipache configuration for the vision-api ./config/server.json
. Please note that we are running vision-api
on port 3001
.
{ "server": { "accessLog": "hipache_access.log", "port": 3001, "workers": 5, "maxSockets": 100, "deadBackendTTL": 30, "address": ["127.0.0.1"], "address6": ["::1"] }, "redisHost": "127.0.0.1", "redisPort": 6379, "redisDatabase": 0 }
We will need to revisit GitHub and change the urls under settings/applications/developer applications/vision
to https://web.vision.net:8443
.
Let's update the vision-web
configuration ./config/*.json
, and change the GitHub authentication urls to web.vision.net
.
"auth": { "homepage": "https://web.vision.net:8443" , "callback": "https://web.vision.net:8443/auth/github/callback" , "clientId": "5bb691b4ebb5417f4ab9" , "clientSecret": "44c16f4d81c99e1ff5f694a532833298cae10473" }
Let's also update the API url
configuration in the same set of config files:
"api": { "url": "http://airasoul:[email protected]:3001" }
Our final change will allow us to support multiple ports for each application; we will change the port setting in the Express server ./lib/express/index.js
, so that it checks process.env.PORT
for a port number:
app.set('port', process.env.PORT || config.get('express:port'));
We now start the process of running our application under a load balancer. In order to start the Hipache load balancer for vision-api
, run the following commands:
cd vision-web hipache --config ./config/server.json
In order to start the Hipache load balancer for vision-web
, we run the following commands:
cd vision-api hipache --config ./config/server.json
So, we now have a running Hipache instance for vision-api
and another for vision-web
. Let's create a vhost in Redis and associate the Hipache instance with a series of servers. Now run the redis command line interface:
redis-cli
First off, let's get the vision-web
application up and running and assign a backend running on port 3003
to web.vision
:
rpush frontend:web.vision.net web.vision rpush frontend:web.vision.net http://127.0.0.1:3003
Let's review the configuration for web.vision
:
lrange frontend:web.vision.net 0 -1
Let's get the vision-api
application up and running and assign a backend running on port 3005
to api.vision
:
rpush frontend:api.vision.net api.vision rpush frontend:api.vision.net http://127.0.0.1:3005
Let's review the configuration for api.vision
:
lrange frontend:api.vision.net 0 -1
Let's run the application under a load balancer, set the PORT
environment variable and set NODE_ENV to production
when running npm start
:
/vision-web/NODE_ENV=production PORT=3003 npm start /vision-api/NODE_ENV=production PORT=3005 npm start /vision-worker/npm start
We now have a vision application running under a load balancer, go visit https://web.vision.net:844
3
. In order to add more backends to our load balancer, let's start vision-api
and vision-web
under another port:
/vision-web/NODE_ENV=production PORT=3004 npm start /vision-api/NODE_ENV=production PORT=3006 npm start
When we run the following commands, the backends running on ports 3004
and 3006
will be added to the load balancer:
rpush frontend:web.vision.net http://127.0.0.1:3004 rpush frontend:api.vision.net http://127.0.0.1:3006
18.223.184.42