ionic-cli: Ionic serve launches browser to "Invalid Host header" only
Bug Report
Ionic Info
Ionic:
ionic (Ionic CLI) : 4.0.1 (/home/david/.nvm/versions/node/v9.11.2/lib/node_modules/ionic)
Ionic Framework : @ionic/angular 4.0.0-beta.0
@angular-devkit/core : 0.7.0-rc.3
@angular-devkit/schematics : 0.7.0-rc.3
@angular/cli : 6.0.8
@ionic/ng-toolkit : 1.0.0
@ionic/schematics-angular : 1.0.1
System:
NodeJS : v9.11.2 (/home/david/.nvm/versions/node/v9.11.2/bin/node)
npm : 5.6.0
OS : Linux 4.14
Describe the Bug
In a new Ionic v4 beta project when I simply run ionic serve
it launches a browser but only displays the message “Invalid Host header” as the only page content.
Steps to Reproduce Steps to reproduce the behavior:
(Follow exact steps in docs for a new v4 beta project.)
npm install -g ionic
ionic start myApp tabs --type=angular
ionic serve
- See error in browser
Related Code
N/A
Expected Behavior
That it launches the app content in the browser.
Additional Context
I’m on a Pixelbook so running inside a LXC container so this issue might be limited to containers.
Maybe a workaround would be to configure webpack to work with the Chrome OS Linux LXC container (aka Crostini) default hostname of penguin.linux.test
?
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 1
- Comments: 29 (12 by maintainers)
Hello all (especially @DavidFrahm @eslindsey @philips @kevin-brotcke) 👋
I have implemented the
--public-host
option for the Ionic CLI which should resolve all issues here. This option provides a way for devs to specify which host the browser should use, regardless of which host the dev server is bound to.Please help me test this PR by installing a test version of the Ionic CLI:
Couple things to note:
--address
is now named--host
localhost
, not0.0.0.0
ionic serve --host=0.0.0.0 --public-host=<hostname>
to testLet me know what you think! 💙
I used:
ionic serve --port 8080 --address 0.0.0.0 -- --disableHostCheck true
@davidfrahm using a hostname will only work if the hostname is resolvable at the server and the client, which can be complicated if you’re not testing locally. But here are a few pointers:
If the container is
penguin.linux.test
, then you should be able toping penguin.linux.test
successfully. If you can’t, you need to addto your
/etc/hosts
on the server.Additionally, if you are testing from non-local, then you need to resolve the name there as well, so you would need to add
to that machine’s
/etc/hosts
.That’s a lot of work, so usually if you get to the point where you need that kind of testing, you’d add something like dnsmasq to take care of those details for you.
Regardless, the name you specify in
--address
MUST be resolvable on the server and client in order for everything to work.Edit: I often see problems like this if a host name is set in
/etc/hostname
but doesn’t have a corresponding entry in/etc/hosts
. The first sets the machine’s hostname, but that doesn’t automatically make it reachable or resolvable. For that, you need the second step. Doing one but not the other can lead to a plethora of mysterious Linux problems. Ionic is probably trying to usepenguin.linux.test
because that’s what you’ve told Linux this machine’s name is. But your DNS server (orhosts
file if local) are unaware of that, so the name can’t be resolved.It’s also worth noting that if you get to the point of using TLS/SSL/HTTPS, you will need to be able to resolve by name, because using the IP address won’t match the certificate and you’ll get security errors.
@eslindsey So actually the default for
ionic serve
is to spin up a server on all network interfaces (0.0.0.0
), allowing testing from the network.localhost
will resolve, but other hosts/addresses will resolve as well.ionic serve --help
does make this clear:I had the same problem, and adding the
--address
parameter did solve it for me.Ok here’s how: create
/vue.config.js
relative to project rootdocs: https://webpack.js.org/configuration/dev-server/#devserverallowedhosts
The default host for
ionic serve
is0.0.0.0
, which should be accessible on all interfaces, including the alias ‘localhost’. If your computer can’t resolvelocalhost
, then the value needs to be changed.