Angular 21.2.0 SSR使用ng serve --host 0.0.0.0无法访问的问题咨询
Angular 21.2.0 SSR使用ng serve --host 0.0.0.0无法访问的问题咨询
我在使用Angular 21.2.0 SSR时遇到了一个访问限制问题,想向社区请教:
当我通过ng serve --host 0.0.0.0启动项目后,尝试从浏览器访问http://localhost:4200/时,会收到如下错误提示:
ERROR: Bad Request ("http://localhost:4200/").
URL with hostname "localhost" is not allowed.
For more information, see https://angular.dev/best-practices/security#preventing-server-side-request-forgery-ssrf
我的实际需求是在同一WiFi环境下用手机测试项目,所以一开始尝试在angular.json的配置中添加允许的主机列表:
"options": { "security": { "allowedHosts": [ "http://localhost:4200", "http://localhost", "localhost", "localhost:4200", "http://localhost:4200/*", "http://localhost:4200/" ] }, // rest of config
但配置后问题并没有得到解决。
后来我尝试启动时指定自己设备的具体IP地址:
ng serve --host 192.168.13.17
此时通过http://192.168.13.17:4200/就能正常访问项目了,手机端也能在同一WiFi下打开。
现在我有两个疑问:
- 这是不是Angular新版本的默认行为?以后是不是都无法通过
0.0.0.0来启动服务并正常访问了? - 有没有办法在使用
0.0.0.0启动服务的前提下,解决localhost访问被拦截的问题?
备注:内容来源于stack exchange,提问作者Sidharth Bajpai
相关产品推荐
相关产品推荐

