Skip to content

Commit

Permalink
Merge pull request #1743 from Volune/feature-proxies-changeorigin
Browse files Browse the repository at this point in the history
Feature(proxy): Allow to configure changeOrigin option of http-proxy
  • Loading branch information
dignifiedquire committed Dec 3, 2015
2 parents 1d8ca12 + f329e96 commit e8fdf38
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
14 changes: 12 additions & 2 deletions docs/config/01-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,21 @@ Note: Using `'https:'` requires you to specify `httpsServerOptions`.

**Description:** A map of path-proxy pairs.

The proxy can be specified directly by the target url or path, or with an object to configure more options. The available options are:

- `target` The target url or path (mandatory)
- `changeOrigin` Whether or not the proxy should override the Host header using the host from the target (default `false`)

**Example:**
```javascript
proxies: {
'/static': 'http://gstatic.com',
'/web': 'http://localhost:9000'
'/static': 'http://gstatic.com',
'/web': 'http://localhost:9000',
'/img/': '/base/test/images/',
'/proxyfied': {
'target': 'http://myserver.localhost',
'changeOrigin': true
}
},
```

Expand Down
8 changes: 7 additions & 1 deletion lib/middleware/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ var parseProxyConfig = function (proxies, config) {
return []
}

return _.sortBy(_.map(proxies, function (proxyUrl, proxyPath) {
return _.sortBy(_.map(proxies, function (proxyConfiguration, proxyPath) {
if (typeof proxyConfiguration === 'string') {
proxyConfiguration = {target: proxyConfiguration}
}
var proxyUrl = proxyConfiguration.target
var proxyDetails = url.parse(proxyUrl)
var pathname = proxyDetails.pathname

Expand All @@ -40,6 +44,7 @@ var parseProxyConfig = function (proxies, config) {
(proxyDetails.protocol === 'https:' ? '443' : '80')
var https = proxyDetails.protocol === 'https:'

var changeOrigin = 'changeOrigin' in proxyConfiguration ? proxyConfiguration.changeOrigin : false
var proxy = httpProxy.createProxyServer({
target: {
host: hostname,
Expand All @@ -48,6 +53,7 @@ var parseProxyConfig = function (proxies, config) {
protocol: protocol
},
xfwd: true,
changeOrigin: changeOrigin,
secure: config.proxyValidateSSL
})

Expand Down
18 changes: 17 additions & 1 deletion test/unit/middleware/proxy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('middleware.proxy', () => {
expect(parsedProxyConfig[0].proxy).to.exist
})

it('should set defualt http port', () => {
it('should set default http port', () => {
var proxy = {'/base/': 'http://localhost/'}
var parsedProxyConfig = m.parseProxyConfig(proxy, {})
expect(parsedProxyConfig).to.have.length(1)
Expand Down Expand Up @@ -291,6 +291,22 @@ describe('middleware.proxy', () => {
expect(parsedProxyConfig[1].proxy).to.exist
})

it('should accept object for proxy config', () => {
var proxy = {
'/base/': {target: 'http://localhost:8000/'}
}
var parsedProxyConfig = m.parseProxyConfig(proxy, {})
expect(parsedProxyConfig).to.have.length(1)
expect(parsedProxyConfig[0]).to.containSubset({
host: 'localhost',
port: '8000',
baseUrl: '/',
path: '/base/',
https: false
})
expect(parsedProxyConfig[0].proxy).to.exist
})

it('should handle empty proxy config', () => {
expect(m.parseProxyConfig({})).to.deep.equal([])
})
Expand Down

0 comments on commit e8fdf38

Please sign in to comment.