Angular 21.2.0的 SSR:更新到 ^21.2.0版本后,ng serve --host 0.0.0.0无法工作

前端开发 2026-07-12

它启动了,但当我尝试从浏览器访问时,出现错误-

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

我用它在同一Wi‑Fi的手机上测试。

我已经尝试在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/

这是从现在起的新行为吗?我们不能使用0.0.0.0吗?

解决方案

这对我有用:

"options": {
    "allowedHosts": ["localhost", "127.0.0.1", "0.0.0.0"]
},
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章