Skip to content

Commit 404818b

Browse files
committed
Allow forwarding for x-forwarded-[for|port|proto] to enabled layering of http-proxies.
1 parent baf0b9e commit 404818b

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Diff for: lib/node-http-proxy.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ HttpProxy.prototype.close = function () {
328328
// options.host {string} Host of the proxy target.
329329
// options.buffer {Object} Result from `httpProxy.buffer(req)`
330330
// options.https {Object|boolean} Settings for https.
331+
// options.allow_xforwarded_headers {boolean} Don't clobber x-forwarded headers to allow layered proxies.
331332
//
332333
HttpProxy.prototype.proxyRequest = function (req, res, options) {
333334
var self = this, errState = false, location, outgoing, protocol, reverseProxy;
@@ -340,6 +341,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
340341
options = options || {};
341342
options.host = options.host || this.target.host;
342343
options.port = options.port || this.target.port;
344+
options.allow_xforwarded_headers = options.allow_xforwarded_headers || false;
343345

344346
//
345347
// Check the proxy table for this instance to see if we need
@@ -379,9 +381,11 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
379381
// * `x-forwarded-proto`: Protocol of the original request
380382
// * `x-forwarded-port`: Port of the original request.
381383
//
382-
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress;
383-
req.headers['x-forwarded-port'] = req.connection.remotePort || req.connection.socket.remotePort;
384-
req.headers['x-forwarded-proto'] = res.connection.pair ? 'https' : 'http';
384+
if (options.allow_xforwarded_headers == true) {
385+
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress;
386+
req.headers['x-forwarded-port'] = req.connection.remotePort || req.connection.socket.remotePort;
387+
req.headers['x-forwarded-proto'] = res.connection.pair ? 'https' : 'http';
388+
}
385389

386390
//
387391
// Emit the `start` event indicating that we have begun the proxy operation.

0 commit comments

Comments
 (0)