Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Invalid Host header from webpack-dev-server

ErrorInvalid Host header

Short answer

The dev server checks the Host header of every request. It accepts localhost and the hosts you list. Other host names are refused with Invalid Host header. Add the domain to devServer.allowedHosts, set client.webSocketURL behind a proxy, and avoid the old disableHostCheck option.

  • Applies to webpack-dev-server 5, webpack 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Note the exact host name in the browser address bar, then look at what the config allows.

    grep -n -A6 "devServer" webpack.config.*
  2. 2Match what you see, then jump to its fix

    1. 1The page works on http://localhost:8080 but not on a custom name such as app.dev.example.com.The domain you open is not in allowedHostsFix
    2. 2The page loads, but the live-reload connection fails or reports the same error.A proxy or tunnel is in front, so the browser connects to another hostFix
    3. 3The option is set to true but the error appears after an upgrade.The config still uses disableHostCheck from webpack-dev-server 3Fix

What does the error look like?

  • Invalid Host header

Cause 1 of 3

The domain you open is not in allowedHosts

1Check

With the default auto setting, only localhost, the configured host and the web socket host name are allowed.

grep -n "allowedHosts\|host:" webpack.config.*

2Fix

List the name. A leading dot allows the domain and every sub-domain of it.

devServer: {
  allowedHosts: ['app.dev.example.com', '.tunnel.example.com'],
},

3Verify

Restart the server and open the custom name.

npx webpack serve --allowed-hosts .tunnel.example.com

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

A proxy or tunnel is in front, so the browser connects to another host

1Check

The docs say client.webSocketURL is for the case where the dev server is proxied and the client script cannot work out where to connect. In auto mode its host name is also allowed.

grep -n -B1 -A6 "webSocketURL" webpack.config.*

2Fix

Point the client at the public address, or let the browser fill in protocol, host name and port.

devServer: {
  client: {
    webSocketURL: 'auto://0.0.0.0:0/ws',
  },
},

3Verify

The socket connects through the proxy and live reload works.

npx webpack serve

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

The config still uses disableHostCheck from webpack-dev-server 3

1Check

The migration guide says disableHostCheck was removed in favour of allowedHosts all.

grep -rn "disableHostCheck" webpack.config.* package.json

2Fix

Replace it. The docs warn that skipping the host check exposes the server to DNS rebinding, so use it only on a trusted network and prefer an explicit list.

devServer: {
  allowedHosts: 'all',
},

3Verify

Requests with any host name are now accepted.

npx webpack serve --allowed-hosts all

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Keep an explicit allowedHosts list per environment instead of all.
  • Share a staging tunnel by a stable domain, not a new random one each time.
  • Search config for removed options after every major dev-server upgrade.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.