Webpack-dev-server: Can webpack-dev-server set Host header when proxy to backend?

Created on 22 Feb 2017  路  4Comments  路  Source: webpack/webpack-dev-server

proxy config:
proxy: { "/api/": { "target": "http://www.b.com/" } }

from www.b.com server access log, the $host is localhost(develop enviroment)
but www.b.com server expected that the $host should be www.b.com.

Can webpack-dev-server set Host header when proxy to backend?
thanks

Most helpful comment

Since this is the top result for "webpack dev server proxy headers", here's the solution:

module.exports = {
  // ... other webpack settings
  devServer: {
    proxy: {
      '**': {
        target: 'http://some-host.test:1234',
        headers: {
          'X-Forwarded-For': '1.2.3.4'
        }
      }
    }
  }
};

Here we're proxying everything to some-host.text on port 1234 and sending along the X-Forwarded-For header with 1.2.3.4.

You have to set headers for each proxy object, so if you have several proxy objects to deal with you might create a headers constant and use that with each proxy object:

const proxy_headers = {
  'X-Forwarded-For': '1.2.3.4',
  'X-Another-Header': 'Hello'
};

module.exports = {
  // ... other webpack settings
  devServer: {
    proxy: {
      'api1/': {
        target: 'http://some-host1.test:1234',
        headers: proxy_headers
      },
      'api2/': {
        target: 'http://some-host2.test:5678',
        headers: proxy_headers
      }
    }
  }
};

All 4 comments

See the options in https://github.com/chimurai/http-proxy-middleware. Closing since this is a question.

Since this is the top result for "webpack dev server proxy headers", here's the solution:

module.exports = {
  // ... other webpack settings
  devServer: {
    proxy: {
      '**': {
        target: 'http://some-host.test:1234',
        headers: {
          'X-Forwarded-For': '1.2.3.4'
        }
      }
    }
  }
};

Here we're proxying everything to some-host.text on port 1234 and sending along the X-Forwarded-For header with 1.2.3.4.

You have to set headers for each proxy object, so if you have several proxy objects to deal with you might create a headers constant and use that with each proxy object:

const proxy_headers = {
  'X-Forwarded-For': '1.2.3.4',
  'X-Another-Header': 'Hello'
};

module.exports = {
  // ... other webpack settings
  devServer: {
    proxy: {
      'api1/': {
        target: 'http://some-host1.test:1234',
        headers: proxy_headers
      },
      'api2/': {
        target: 'http://some-host2.test:5678',
        headers: proxy_headers
      }
    }
  }
};

For me it worked with Host header, like this:

target: 'http://some-host.test:1234',
headers: {
  'Host': 'some-host.test'
}    

Not sure in which version it was introduced but dev-server version 3.2 and above:

target: 'http://some-host.test:1234',
changeOrigin: true,
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wojtekmaj picture wojtekmaj  路  3Comments

movie4 picture movie4  路  3Comments

subblue picture subblue  路  3Comments

Jack-Works picture Jack-Works  路  3Comments

daryn-k picture daryn-k  路  3Comments