【问题标题】:When running angular project on localhost, I can type in any url for my website, but on apache server I can't在 localhost 上运行 Angular 项目时,我可以为我的网站输入任何 url,但在 apache 服务器上我不能
【发布时间】:2018-07-23 21:12:55
【问题描述】:

我正在本地开发机器上使用 ng serve 运行 Angular 6 项目。例如,我有以下端点:

  • /登录
  • /购物车

当我的项目在 localhost:4200 上运行时,我可以输入以下任何一个,它们都是有效的(浏览器会成功加载相应的页面):

  • 本地主机:4200
  • localhost:4200/登录
  • 本地主机:4200/购物车

但是,当我构建 dist 文件夹并将内容推送到我的生产服务器时,我尝试通过相同的 url 访问该站点,但它不起作用:

那些不工作的响应一个 404 not found 错误。在控制台中它说:

加载资源失败:服务器响应状态为 404(未找到)

本地开发服务器和生产服务器上的行为有何不同?如何允许用户直接路由到特定端点,例如 http://xxx.xxx.xx.xx/cart

这也将解决我的其他问题。我添加了一个功能,如果用户实际上输入了错误的 url,他们将收到 404 错误(如预期的那样)。我在我的 app-routing.module.ts 文件中配置了 404 错误将被重定向到自定义组件。但是,这不再有效。

这是我按项目构建的方式:

ng build --configuration=staging  # --configuration=staging specifies that the build should use the staging environment listed in environments folder

文件存储在服务器上的 /var/www/html 文件夹中,并且正在运行 apache2。

【问题讨论】:

    标签: angular url-routing angular-routing


    【解决方案1】:

    如文档中所述,角度服务器必须将所有请求重定向到 index.html 每个服务器的配置形式。

    https://angular.io/guide/deployment#routed-apps-must-fallback-to-indexhtml

    【讨论】:

      【解决方案2】:

      一切都必须通过 index.html 路由才能工作请使用下面链接中的配置使其工作https://github.com/mgechev/angular-seed/wiki/Deploying-prod-build-to-Apache-2

      【讨论】:

        猜你喜欢
        • 2020-04-21
        • 2021-07-02
        • 2017-09-10
        • 2021-12-14
        • 2021-12-25
        • 2019-01-15
        • 1970-01-01
        • 1970-01-01
        • 2012-05-30
        相关资源
        最近更新 更多