You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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下打开。

现在我有两个疑问:

  1. 这是不是Angular新版本的默认行为?以后是不是都无法通过0.0.0.0来启动服务并正常访问了?
  2. 有没有办法在使用0.0.0.0启动服务的前提下,解决localhost访问被拦截的问题?

备注:内容来源于stack exchange,提问作者Sidharth Bajpai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:09:52