如何配置 Angular 17 开发服务器将 HTTP 重定向到 HTTPS?

问题描述 投票:0回答:1

我已成功配置 Angular 开发服务器以使用带有 SSL 的 HTTPS:

"port": 4200,
"ssl": true,
"sslKey": "./tools/.ssl/localhost+2-key.pem",
"sslCert": "./tools/.ssl/localhost+2.pem"

当我导航到 https://localhost:4200 时,页面加载正确。但是,当我导航到 http://localhost:4200 时,收到 404 错误。如何配置 Angular 17 开发服务器将 HTTP 重定向到 HTTPS?是否可以在 Angular 中实现此目的,或者我是否需要使用脚本标签进行重定向?

angular
1个回答
0
投票

我认为 Angular 没有为本地环境提供任何此类配置,但您可以使用 js 来更新 index.html 中的基本 url

示例 - 将其添加到index.html

  <script>
    if (window.location.protocol !== 'https:') {
      window.location.href = 'https://' + window.location.hostname + (window.location.port ? ':' + window.location.port : '') + window.location.pathname + window.location.search;
    }
  </script> 
© www.soinside.com 2019 - 2024. All rights reserved.