Skip to content

Commit 1cc1f90

Browse files
committed
Merge pull request #275 from jfromaniello/patch-1
add "Using two certificiates" to the https section of the readme.md
2 parents 54f8371 + 0b6c7f5 commit 1cc1f90

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,67 @@ http.createServer(function (req, res) {
293293
}).listen(8000);
294294
```
295295

296+
### Using two certificates
297+
298+
Suppose that your reverse proxy will handle HTTPS traffic for two different domains `fobar.com` and `barbaz.com`.
299+
If you need to use two different certificates you can take advantage of [Server Name Indication](http://en.wikipedia.org/wiki/Server_Name_Indication).
300+
301+
``` js
302+
var https = require('https'),
303+
path = require("path"),
304+
fs = require("fs"),
305+
crypto = require("crypto");
306+
307+
//
308+
// generic function to load the credentials context from disk
309+
//
310+
function getCredentialsContext(cer){
311+
return crypto.createCredentials({
312+
key: fs.readFileSync(path.join(__dirname, 'certs', cer + '.key')),
313+
cert: fs.readFileSync(path.join(__dirname, 'certs', cer + '.crt'))
314+
}).context;
315+
}
316+
317+
//
318+
// A certificate per domain hash
319+
//
320+
var certs = {
321+
"fobar.com": getCredentialsContext("foobar"),
322+
"barbaz.com": getCredentialsContext("barbaz")
323+
};
324+
325+
//
326+
// Proxy options
327+
//
328+
var options = {
329+
https: {
330+
SNICallback: function(hostname){
331+
return certs[hostname];
332+
}
333+
},
334+
hostnameOnly: true,
335+
router: {
336+
'fobar.com': '127.0.0.1:8001',
337+
'barbaz.com': '127.0.0.1:8002'
338+
}
339+
};
340+
341+
//
342+
// Create a standalone HTTPS proxy server
343+
//
344+
httpProxy.createServer(options).listen(8001);
345+
346+
//
347+
// Create the target HTTPS server
348+
//
349+
http.createServer(function (req, res) {
350+
res.writeHead(200, { 'Content-Type': 'text/plain' });
351+
res.write('hello https\n');
352+
res.end();
353+
}).listen(8000);
354+
355+
```
356+
296357
### Proxying to HTTPS from HTTPS
297358
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`.
298359

0 commit comments

Comments
 (0)