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

99封情书 提交于 2020-05-09 17:56:05

问题


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

Results on my website:

Invalid Host header


回答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



回答2:


You need to specify the host

ng serve --port 8080 --host 123.34.56.78



回答3:


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.




回答4:


This works for me :

ngrok http --host-header=rewrite PORT

for instance :

ngrok http --host-header=rewrite 4200



回答5:


Using the command below works for me.

ng serve --host 0.0.0.0 --port 8080  --public ipAddress



回答6:


In my case, I use my host name to update /etc/hosts file.

vi /etc/hosts

and add your host name last line.

127.0.0.1 myHostName.com

to connect my server,

ng serve -o

An error occurred when connecting to myHostName.com:4200.

so, I did like this,

ng serve --host 0.0.0.0 --disableHostCheck true

Reconnecting to myHostName.com:4200 :)




回答7:


You need to run following : ng serve --port 8080 --publicHost 123.34.56.78 ( your server ip or host name). works for me .




回答8:


I was facing same error after deploying my angular app on AWS EC2 instance having bitnami image. Then i launched my app with below command and it worked fine.

ng serve --host 0.0.0.0 --port 8080 --disableHostCheck true



回答9:


Use the command below works for me for Angular CLI: 6.0.7

ng serve --host 0.0.0.0 --port 8080 --disableHostCheck



回答10:


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

"disableHostCheck": true

Got the answe from this Github Issue Page




回答11:


Tested on the below version of Angular and server Amazon.

Angular CLI: 6.0.8
Node: 8.11.3
OS: linux x64
Angular: 5.2.8

ng command options has been changed, now use configuration instead of env. The command which worked for me is

 ng serve --configuration=dev  --port 4009 --host 0.0.0.0 --publicHost myhost.com

Never use --disable-host-check on a public server. Angular will warn you if you use this. It may work but it is a serious security issue. You will get this message if you use that flag.

WARNING: Running a server with --disable-host-check is a security risk. See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a for more information.

Please take some time to read that article it is worth reading.




回答12:


This was actually solved in one of the youtube video comments thanks to Mateusz Sawa

Go here to check it out.

-- specifyng a host in package.json does not work anymore --

just to note that it is not necessary to use the command that was used in the video, bellow instructions are enough to make the app work using regular angular-cli development

first you need to find the hosts in the etc/ folder

type cd .. in the console until you reach the root of the linux machine and check always with ls what is listed

if you see the hosts file, than you are in the right location and then use sudo vi hosts to edit it

add the line bellow all ip addresses "0.0.0.0 yourworkspace-yourusername.c9users.io" (without quotes of course :-) )

also in package.json change "start": "ng serve -H yourworkspace-yourusername.c9users.io"

then you just need to go to the application folder and start the app in the terminal with npm start

checked it just now and it worked




回答13:


Using the command below with the latest version of Angular CLI works for me.

ng serve --host 0.0.0.0 --public-host <workspace>-<username>.c9users.io



回答14:


Below both solutions will work. In command line :

ng serve --public --host yourip --port yourportnumber

ng serve --host 0.0.0.0 --port yourport --disableHostCheck true




回答15:


I got same error. Public option solved problem for me like below:

ng serve --host 0.0.0.0 --port 8080 --live-reload-port 8081 --public $C9_HOSTNAME



回答16:


Refer to: https://github.com/angular/angular-cli/issues/6070

use the public flag. for example: ng serve --host <workspace>-<username>.c9users.io --public




回答17:


You should just change 0.0.0.0 to your local IP address like 192.168.1.42 and it should work.

ng serve --host 192.168.1.42



回答18:


The thing is you can only access to link you have specified in --public parameter. In my case, I have provided the IP address in --public and trying to access it with the domain I registered against that IP. But ng binds it for the only host provided in --public.

To solve this problem, I supplied one more parameter: disable-host-check. Complete command: ng serve --disable-host-check --host 0.0.0.0 --port 3100 --public x.x.x.x

To know more, click here




回答19:


I was getting this error when trying to access our app using the localtunnel utility.

@tarn 's suggestion (from a comment on the accepted answer) of adding --disableHostCheck true to the ng serve command did the trick.




回答20:


Here is solution if anybody still stuck in invalid host header issue:

Run ng eject it will create the webpack.config.js. Run npm install if it is asking. Add below "disableHostCheck":true to devServer in webpack.config.js. Like below:

"devServer": {
    "historyApiFallback": true,
    "disableHostCheck" : true //<-- add this line
  }

Then in package.json file change start option under ng like this

"start": "webpack-dev-server --host 0.0.0.0 --port YOURPORT --public YOURIP:YOURPORT"

Now run the angular with npm start command. Thats it.




回答21:


thanks for all the workarounds, --disable-host-check works for me, but i don't understand why i have to use that workaround, why can't i just ng serve like any other ng new application.




回答22:


Give this a shot

ng serve --host <private IP> --port <host port> --public-host <public IP>



回答23:


Change the line 425 in file "node_modules/webpack-dev-server/lib/Server.js" from false to true. i.e,

Before: return false;

Updated: return true;



来源:https://stackoverflow.com/questions/43677629/invalid-host-header-when-running-angular-cli-development-server-c9-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!