"Invalid Host header" when running Angular/cli development server c9.io

126,478

Solution 1

The --disable-host-check flag works fine if you need to run from Cloud9.

I use the following command:

ng serve --open --host $IP --port $PORT --disable-host-check

Solution 2

In angular.json, add the following under architect -> serve -> options

"disableHostCheck": true

Got the answer from this Github Issue Page

Solution 3

You need to specify the host

ng serve --port 8080 --host 123.34.56.78

Solution 4

This works for me :

ngrok http --host-header=rewrite PORT

for instance :

ngrok http --host-header=rewrite 4200

Solution 5

See this issue

Edit the following line in node_modules/webpack-dev-server/lib/Server.js (line 425), change to:

return true;

I am using cloud9 IDE then run: ng serve --port 8080 --host 0.0.0.0. Now works fine.

Share:
126,478
Olejs
Author by

Olejs

Updated on August 20, 2022

Comments

  • Olejs
    Olejs over 1 year

    Current command: ng serve --host --public $IP:$PORT

    Results on my website:

    Invalid Host header

  • Teddy Markov
    Teddy Markov almost 7 years
    The above solution might work too but this is what I needed.
  • tarn
    tarn almost 7 years
    You can run without edit code by append --disableHostCheck true ng serve --host 0.0.0.0 --port 8080 --disableHostCheck true
  • rakesh kashyap
    rakesh kashyap over 6 years
    change the following line in node_modules/webpack-dev-server/lib/Server.js: if(this.disableHostCheck) return true; change to return true;
  • niaomingjian
    niaomingjian over 6 years
    It works. But where can I find out the explanation about the '--public' option online? I only found out the '--public-host' option
  • Nitin .
    Nitin . over 6 years
    ng serve --port 8080 --publicHost 0.0.0.0
  • jmq
    jmq over 6 years
    See this Angular CLI GitHub issue regarding disableHostCheck's potential risks, just to be aware of what you're doing.
  • Matthew Dolman
    Matthew Dolman almost 6 years
    Adetiloye's answer is much better
  • Krishnadas PC
    Krishnadas PC almost 6 years
    It will work but if you use --disable-host-check you will get a warning in the console. WARNING: Running a server with --disable-host-check is a security risk. See medium.com/webpack/… for more information.
  • Mathews Sunny
    Mathews Sunny over 5 years
    Add some more description.
  • edi
    edi over 5 years
    this option is not available anymore
  • Cel
    Cel over 5 years
    this worked for me, but it was necessary to include the exact subdomain used in the browser for public-host: ng serve --host 0.0.0.0 --public-host sub.myhost.com
  • Vinay Pandya
    Vinay Pandya almost 5 years
    this is worked for me ng serve --host 0.0.0.0 --disableHostCheck true. thank you @CookAtRice
  • Ubaid Azad
    Ubaid Azad almost 5 years
    Run ng serve --host 0.0.0.0 --disable-host-check
  • Pablo W.
    Pablo W. almost 5 years
    you should not be touching node vendors
  • Cec
    Cec over 4 years
    modifying node_modules instead of using proper ng-cli parameters is absurdly bad practice
  • Tom Doe
    Tom Doe about 4 years
    Just want to point out that this resolves the issue for running locally on the machine. I'm not sure if it solves it for Cloud9 per the OP original question above.
  • jowey
    jowey about 4 years
    This is the right an clean solution if you want to enable it on every ng serve (and probably you want to in development)
  • LuizLoyola
    LuizLoyola over 3 years
    Not the answer the OP needed, but thanks for adding it anyway. Works for ngrok tunneling without changing project settings.
  • danday74
    danday74 over 3 years
    a better solution
  • JellicleCat
    JellicleCat about 3 years
    Is there a way to configure this for the development environment only?
  • ruwan800
    ruwan800 about 3 years
    @JellicleCat Set "disableHostCheck": false under architect -> serve -> configurations -> production
  • vanduc1102
    vanduc1102 about 2 years
    I am building a Shopify app, I have to run yarn start frontend --public-host=abc-xyz.ngrok.io , with this answer, i can just yarn start frontend . I think the issue should be fixed with disableHostCheck.