Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New defaultPort option allows sc-proxy to be the front end proxy for …

…Apache (or some other server), forwarding traffic not intended for one of your node sites
  • Loading branch information...
commit 614fcb87cf980052a5656dcc9a11c1f51d0db5e6 1 parent f85cb9a
@boutell boutell authored
Showing with 35 additions and 5 deletions.
  1. +5 −3 README.md
  2. +6 −1 sc-proxy/config-example.js
  3. +24 −1 sc-proxy/server.js
View
8 README.md
@@ -111,11 +111,13 @@ If `sc-proxy` is asked to access a site that isn't part of its current configura
Configuring sc-proxy
====================
-To configure `sc-proxy`, copy the file `config-example.js` to `config.js` and change the `domain` setting to match your needs. Also set `ip` to the IP address you want to listen on; you can set `0.0.0.0` to respond on all interfaces, but that will conflict if you have an Apache server on the same system, so you should probably use a separate IP address just for your node projects (ask your web host for an extra IP address). You can also change the port from port 80 for testing purposes if you don't have an extra IP yet, although there is not much point in using `sc-proxy` if you don't plan to get one eventually and switch back to port 80.
+To configure `sc-proxy`, copy the file `config-example.js` to `config.js` and change the `domain` setting to match your needs. Also set `ip` to the IP address you want to listen on; you can set `0.0.0.0` to respond on all interfaces. If you want to listen on a specific IP address to avoid a conflict with a second IP address reserved for Apache, you can do that as well. You can also change the port from port 80 for testing purposes, although there is not much point in using `sc-proxy` if you don't plan to eventually configure it to bind on port 80.
-If Apache is on the same server, you will also need to configure Apache to listen on just one IP, or at any rate to not listen on the IP set aside for `sc-proxy`.
+Similarly, if Apache is on the same server, you will need to configure Apache to listen on a different IP address, or a different port if you use the `defaultPort` setting of `sc-proxy`.
-Then cd to the `sc-proxy` folder and run:
+You can set `defaultPort` to forward traffic to that port if it does not match any of your Stagecoach sites. This allows sc-proxy to act as a front end to Apache, as long as you change Apache's configuration to bind on the port indicated by `defaultPort`. This is one way to avoid a second IP address.
+
+When your configuration is complete, cd to the `sc-proxy` folder and run:
npm install
View
7 sc-proxy/config-example.js
@@ -9,5 +9,10 @@ module.exports = {
bindIp: '0.0.0.0',
// Our example start and stop scripts for stagecoach assume this will be /var/webapps, but you
// can change it if you are consistent. You'll need to edit sc-start-all and sc-stop-all
- appsDir: '/opt/stagecoach/apps'
+ appsDir: '/opt/stagecoach/apps',
+ // If this option is present, forward all traffic that doesn't match for a stagecoach site
+ // to this port. This allows node-http-proxy to bind on the same IP as Apache and forward
+ // traffic it doesn't recognize to Apache. The opposite direction is a bad idea because
+ // Apache's reverse proxy doesn't support WebSockets
+ defaultPort: 9898
};
View
25 sc-proxy/server.js
@@ -22,6 +22,26 @@ setInterval(resetRouter, 60000);
var proxyServer = httpProxy.createServer(options).listen(config.port, config.bindIp);
+var table = proxyServer.proxy.proxyTable;
+// If there is a default port, monkeypatch the getProxyLocation method
+// to respect it when nothing in the routing table matches
+if (config.defaultPort)
+{
+ table.superGetProxyLocation = table.getProxyLocation;
+ table.getProxyLocation = function(req)
+ {
+ var location = table.superGetProxyLocation(req);
+ if (location)
+ {
+ return location;
+ }
+ return {
+ host: '127.0.0.1',
+ port: config.defaultPort
+ }
+ };
+}
+
// Reset the routes on the fly
function resetRouter()
@@ -34,7 +54,10 @@ function resetRouter()
setTimeout(resetRouter, 2000);
return;
}
- proxyServer.proxy.proxyTable.setRoutes(router);
+
+ var table = proxyServer.proxy.proxyTable;
+ table.setRoutes(router);
+
proxyServer.proxy.proxyTable.emit('routes', proxyServer.proxy.proxyTable.router);
}
Please sign in to comment.
Something went wrong with that request. Please try again.