diff --git a/lib/middleware/proxy.js b/lib/middleware/proxy.js index 4f01c3d66..af7aa95a4 100644 --- a/lib/middleware/proxy.js +++ b/lib/middleware/proxy.js @@ -1,4 +1,6 @@ const url = require('url') +const { Agent: httpAgent } = require('http') +const { Agent: httpsAgent } = require('https') const httpProxy = require('http-proxy') const _ = require('lodash') @@ -42,11 +44,14 @@ function parseProxyConfig (proxies, config) { port = config.port } const changeOrigin = proxyConfiguration.changeOrigin || false + const Agent = https ? httpsAgent : httpAgent + const keepAliveAgent = new Agent({ keepAlive: true }) const proxy = httpProxy.createProxyServer({ target: { host: hostname, port, https, protocol }, xfwd: true, changeOrigin: changeOrigin, - secure: config.proxyValidateSSL + secure: config.proxyValidateSSL, + agent: keepAliveAgent }) ;['proxyReq', 'proxyRes'].forEach(function (name) { diff --git a/test/unit/middleware/proxy.spec.js b/test/unit/middleware/proxy.spec.js index 5e97e2290..6bfc2c80c 100644 --- a/test/unit/middleware/proxy.spec.js +++ b/test/unit/middleware/proxy.spec.js @@ -352,4 +352,24 @@ describe('middleware.proxy', () => { it('should handle empty proxy config', () => { expect(m.parseProxyConfig({})).to.deep.equal([]) }) + + it('should use http agent with keepAlive=true', () => { + const proxy = { '/base': 'http://localhost:8000/proxy' } + const parsedProxyConfig = m.parseProxyConfig(proxy, {}) + expect(parsedProxyConfig).to.have.length(1) + expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({ + keepAlive: true, + protocol: 'http:' + }) + }) + + it('should use https agent with keepAlive=true', () => { + const proxy = { '/base': 'https://localhost:8000/proxy' } + const parsedProxyConfig = m.parseProxyConfig(proxy, {}) + expect(parsedProxyConfig).to.have.length(1) + expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({ + keepAlive: true, + protocol: 'https:' + }) + }) })