首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Nginx和角路由(找不到.js文件)

Nginx和角路由(找不到.js文件)
EN

Stack Overflow用户
提问于 2020-04-28 01:20:10
回答 2查看 2.4K关注 0票数 0

我们遇到了一个问题,如何正确显示使用Nginx服务的角度路由的页面。在我们的角度计划中,我们的基础href是"/dashboard“。该项目有两个不同的路线指向“/仪表板/数据-分析”和“/仪表板/数据-可视化”。

下面是我的基本nginx.conf文件:

代码语言:javascript
运行
复制
server{
      listen 80;
      server_name localhost;

      location /dashboard/ {
          alias C:/Users/C123/Desktop/html/dashboard;
          index index.html;
          try_files $url $url/ /dashboard/index.html;
      }
}

当我继续使用"localhost/dashboard/“或"localhost/dashboard/data-analysis”时,任何一个页面都将从“C:/ index.html /C 123/Desktop/html/dashboard/”加载"polyfill.js“"runtime.js”"style.js“等,这些页面都是在index.html的<script>标记中定义的。

此外,Nginx试图从“C:/Users/C 123/nginx/nginx-1.16.1/html/”中加载这些文件,而不是我的别名(C:/Users/C 123/Desktop/html/dashboard)。

是否有一种方法可以在“仪表板/”位置块中定义它将读取别名的index.html中的所有内容?我不想在外部定义根,也不想只为".js“文件定义第二个位置块。

更新(此解决方案对我有效):

代码语言:javascript
运行
复制
server{
      listen 80;
      server_name localhost;

      location /dashboard/ {
          root C:/Users/C123/Desktop/html/
          try_files $uri $uri/ /dashboard/index.html/
      }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-28 13:44:59

您缺少了root指令。

另外,try_files应该尝试加载声明的文件,或者在目录中加载index.html

没有经过测试,但应该有效:

代码语言:javascript
运行
复制
server {
  listen 80;
  server_name localhost;

  location /dashboard/ {
      root C:/Users/C123/Desktop/html;
      try_files $uri $uri/index.html =404;
  }
}

编辑:

如果您试图访问dashboard目录中的文件,而不指定请求中的单词dashboard,则可以为此添加一个要回退的块。

代码语言:javascript
运行
复制
server {
  listen 80;
  server_name localhost;

  # this will serve requests like http://localhost/runtime.js
  location / {
      root C:/Users/C123/Desktop/html/dashboard;
      try_files $uri $uri/index.html =404;
  }
  # this will serve requests like http://localhost/dashboard/
  location /dashboard/ {
      root C:/Users/C123/Desktop/html;
      try_files $uri $uri/index.html =404;
  }
}

请记住,当NGINX选择位置上下文时,从前面提到的位置选择将是最长的匹配

票数 1
EN

Stack Overflow用户

发布于 2021-05-28 12:17:39

您可能会在baseHref和deployUrl中设置angular.json。您可以在projects -> $projectName -> architect -> build ->选项中设置它。

代码语言:javascript
运行
复制
"deployUrl": "/dashboard/",
"baseHref": "/dashboard/",

有关这些参数的更多内容,请参见answer

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61471102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档