Skip to content

add "Using two certificiates" to the https section of the readme.md #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,67 @@ http.createServer(function (req, res) {
}).listen(8000);
```

### Using two certificates

Suppose that your reverse proxy will handle HTTPS traffic for two different domains `fobar.com` and `barbaz.com`.
If you need to use two different certificates you can take advantage of [Server Name Indication](http://en.wikipedia.org/wiki/Server_Name_Indication).

``` js
var https = require('https'),
path = require("path"),
fs = require("fs"),
crypto = require("crypto");

//
// generic function to load the credentials context from disk
//
function getCredentialsContext(cer){
return crypto.createCredentials({
key: fs.readFileSync(path.join(__dirname, 'certs', cer + '.key')),
cert: fs.readFileSync(path.join(__dirname, 'certs', cer + '.crt'))
}).context;
}

//
// A certificate per domain hash
//
var certs = {
"fobar.com": getCredentialsContext("foobar"),
"barbaz.com": getCredentialsContext("barbaz")
};

//
// Proxy options
//
var options = {
https: {
SNICallback: function(hostname){
return certs[hostname];
}
},
hostnameOnly: true,
router: {
'fobar.com': '127.0.0.1:8001',
'barbaz.com': '127.0.0.1:8002'
}
};

//
// Create a standalone HTTPS proxy server
//
httpProxy.createServer(options).listen(8001);

//
// Create the target HTTPS server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('hello https\n');
res.end();
}).listen(8000);

```

### Proxying to HTTPS from HTTPS
Proxying from HTTPS to HTTPS is essentially the same as proxying from HTTPS to HTTP, but you must include the `target` option in when calling `httpProxy.createServer` or instantiating a new instance of `HttpProxy`.

Expand Down