Skip to content

Commit abc01bc

Browse files
committed
[doc] Update README.md to reflect the new HTTPS to HTTP proxy capabilities
1 parent faf2618 commit abc01bc

File tree

1 file changed

+78
-15
lines changed

1 file changed

+78
-15
lines changed

README.md

+78-15
Original file line numberDiff line numberDiff line change
@@ -216,24 +216,55 @@ proxyServerWithForwarding.listen(80);
216216

217217
The forwarding option can be used in conjunction with the proxy table options by simply including both the 'forward' and 'router' properties in the options passed to 'createServer'.
218218

219-
### Using node-http-proxy from the command line
220-
When you install this package with npm, a node-http-proxy binary will become available to you. Using this binary is easy with some simple options:
221219

220+
## Using HTTPS
221+
You have all the full flexibility of node-http-proxy offers in HTTPS as well as HTTP. The two basic scenarios are: with a stand-alone proxy server or in conjunction with another HTTPS server.
222+
223+
### Proxying to HTTP from HTTPS
224+
This is probably the most common use-case for proxying in conjunction with HTTPS. You have some front-facing HTTPS server, but all of your internal traffic is HTTP. In this way, you can reduce the number of servers to which your CA and other important security files are deployed and reduce the computational overhead from HTTPS traffic.
225+
226+
Using HTTPS in `node-http-proxy` is relatively straight-forward:
222227
``` js
223-
usage: node-http-proxy [options]
228+
var fs = require('fs'),
229+
http = require('http'),
230+
https = require('https'),
231+
httpProxy = require('http-proxy');
232+
233+
var options = {
234+
https: {
235+
key: fs.readFileSync('path/to/your/key.pem', 'utf8'),
236+
cert: fs.readFileSync('path/to/your/cert.pem', 'utf8')
237+
}
238+
};
224239

225-
All options should be set with the syntax --option=value
240+
//
241+
// Create a standalone HTTPS proxy server
242+
//
243+
httpProxy.createServer(8000, 'localhost', options).listen(8001);
226244

227-
options:
228-
--port PORT Port that the proxy server should run on
229-
--target HOST:PORT Location of the server the proxy will target
230-
--config OUTFILE Location of the configuration file for the proxy server
231-
--silent Silence the log output from the proxy server
232-
-h, --help You're staring at it
245+
//
246+
// Create an instance of HttpProxy to use with another HTTPS server
247+
//
248+
var proxy = new httpProxy.HttpProxy();
249+
https.createServer(options.https, function (req, res) {
250+
proxy.proxyRequest(req, res, {
251+
host: 'localhost',
252+
port: 8000
253+
})
254+
}).listen(8002);
255+
256+
//
257+
// Create the target HTTPS server for both cases
258+
//
259+
http.createServer(function (req, res) {
260+
res.writeHead(200, { 'Content-Type': 'text/plain' });
261+
res.write('hello https\n');
262+
res.end();
263+
}).listen(8000);
233264
```
234265

235-
### Proxying over HTTPS
236-
You have all the full flexibility of node-http-proxy offers in HTTPS as well as HTTP. The two basic scenarios are: with a stand-alone proxy server or in conjunction with another HTTPS server.
266+
### Proxying to HTTPS from HTTPS
267+
Proxying from HTTPS to HTTPS is essentially the same as proxying from HTTPS to HTTP, but you must include `target` option in when calling `httpProxy.createServer` or instantiating a new instance of `HttpProxy`.
237268

238269
``` js
239270
var fs = require('fs'),
@@ -244,6 +275,9 @@ var options = {
244275
https: {
245276
key: fs.readFileSync('path/to/your/key.pem', 'utf8'),
246277
cert: fs.readFileSync('path/to/your/cert.pem', 'utf8')
278+
},
279+
target: {
280+
https: true // This could also be an Object with key and cert properties
247281
}
248282
};
249283

@@ -255,7 +289,12 @@ httpProxy.createServer(8000, 'localhost', options).listen(8001);
255289
//
256290
// Create an instance of HttpProxy to use with another HTTPS server
257291
//
258-
var proxy = new httpProxy.HttpProxy({ https: true });
292+
var proxy = new httpProxy.HttpProxy({
293+
target: {
294+
https: true
295+
}
296+
});
297+
259298
https.createServer(options.https, function (req, res) {
260299
proxy.proxyRequest(req, res, {
261300
host: 'localhost',
@@ -273,7 +312,7 @@ https.createServer(options.https, function (req, res) {
273312
}).listen(8000);
274313
```
275314

276-
### Proxying WebSockets
315+
## Proxying WebSockets
277316
Websockets are handled automatically when using the `httpProxy.createServer()`, but if you want to use it in conjunction with a stand-alone HTTP + WebSocket (such as [socket.io][5]) server here's how:
278317

279318
``` js
@@ -306,16 +345,40 @@ server.on('upgrade', function(req, socket, head) {
306345
});
307346
```
308347

348+
## Using node-http-proxy from the command line
349+
When you install this package with npm, a node-http-proxy binary will become available to you. Using this binary is easy with some simple options:
350+
351+
``` js
352+
usage: node-http-proxy [options]
353+
354+
All options should be set with the syntax --option=value
355+
356+
options:
357+
--port PORT Port that the proxy server should run on
358+
--target HOST:PORT Location of the server the proxy will target
359+
--config OUTFILE Location of the configuration file for the proxy server
360+
--silent Silence the log output from the proxy server
361+
-h, --help You're staring at it
362+
```
363+
309364
<br/>
310-
### Why doesn't node-http-proxy have more advanced features like x, y, or z?
365+
## Why doesn't node-http-proxy have more advanced features like x, y, or z?
311366

312367
If you have a suggestion for a feature currently not supported, feel free to open a [support issue][6]. node-http-proxy is designed to just proxy http requests from one server to another, but we will be soon releasing many other complimentary projects that can be used in conjunction with node-http-proxy.
313368

314369
## Run Tests
370+
The test suite is designed to fully cover the combinatoric possibilities of HTTP and HTTPS proxying:
371+
372+
1. HTTP --> HTTP
373+
2. HTTPS --> HTTP
374+
3. HTTPS --> HTTPS
375+
4. HTTP --> HTTPS
315376

316377
```
317378
vows test/*-test.js --spec
318379
vows test/*-test.js --spec --https
380+
vows test/*-test.js --spec --https --target=https
381+
vows test/*-test.js --spec --target=https
319382
```
320383
321384
<br/>

0 commit comments

Comments
 (0)