Invalid Host header from webpack-dev-server
Invalid Host headerShort 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.
Fix it fast
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.*2Match what you see, then jump to its fix
- 1
The 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
The 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
The option is set to true but the error appears after an upgrade.The config still uses disableHostCheck from webpack-dev-server 3Fix
- 1
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.comConfirmed by webpack.js.org
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 serveConfirmed by webpack.js.org
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.json2Fix
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 allConfirmed by github.com
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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix