diff --git a/lib/middleware/proxy.js b/lib/middleware/proxy.js index 4f01c3d66..dcc48eee8 100644 --- a/lib/middleware/proxy.js +++ b/lib/middleware/proxy.js @@ -1,4 +1,5 @@ const url = require('url') +const http = require('http') const httpProxy = require('http-proxy') const _ = require('lodash') @@ -42,11 +43,13 @@ function parseProxyConfig (proxies, config) { port = config.port } const changeOrigin = proxyConfiguration.changeOrigin || false + const keepAliveAgent = new http.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..98f7055b4 100644 --- a/test/unit/middleware/proxy.spec.js +++ b/test/unit/middleware/proxy.spec.js @@ -352,4 +352,13 @@ describe('middleware.proxy', () => { it('should handle empty proxy config', () => { expect(m.parseProxyConfig({})).to.deep.equal([]) }) + + it('should use 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 + }) + }) })