【问题标题】:How to access to a specifc route of an angular application with nginx如何使用 nginx 访问 Angular 应用程序的特定路由
【发布时间】:2018-06-26 16:34:18
【问题描述】:

早安

我正在尝试使用 nginx 直接在我的 Angular 应用程序的一页上使用我的路由中的参数进行访问。这个应用不在根目录,而是在一个子目录myapphttp://my_serveur:my_port:/myapp/mypage/myparameter.

当我的 Angular 应用程序位于 nginx html 目录的根目录时,我找到了很多访问特定路由的解决方案。

 location / {
        try_files /$uri /$uri/ /roleapp/index.html;
  }

但是当应用程序在子目录/usr/share/nginx/html/myapp/时我不能这样做

我已经改变了基本的href

ng build --base-href=/myapp/

但是nginx会将应用重定向到根目录的index.html。

我试过了

location /myapp/ {
     #   alias /usr/share/nginx/html/roleapp/
        try_files /myapp/$uri /myapp/$uri/ /myapp/index.html;
     }

但是 Nginx 给我发回错误 500。

你能帮帮我吗?

愚蠢

【问题讨论】:

    标签: angular nginx


    【解决方案1】:

    $uri 变量已包含前导 /,因此您的第一个 try_files 语句实际上应该如下所示:

    try_files $uri $uri/ /roleapp/index.html;
    

    在第二种情况下,$uri 的值已经包含了/myapp/ 前缀,因为只有当$uri/myapp/ 开头时才会输入location /myapp/ { ... } 块。所以你的第二个try_files 语句应该是这样的:

    try_files $uri $uri/ /myapp/index.html;
    

    请参阅this document 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2020-08-31
      • 2021-01-08
      • 2017-04-27
      • 2019-09-26
      • 2021-03-22
      • 2021-03-20
      • 1970-01-01
      • 2020-12-06
      • 2016-04-14
      相关资源
      最近更新 更多