【问题标题】:Nginx config with multiple AngularJS2 Projects using HTML5Mode使用 HTML5Mode 配置多个 AngularJS2 项目的 Nginx 配置
【发布时间】:2017-05-28 15:37:54
【问题描述】:

我的 Nginx 配置有问题。我有两个单独的 angularjs2 项目,它们应该与一个 nginx 实例一起使用。 应用程序分为两个文件夹 /admin 和 /webapp。

现在我要做的是,当我调用 mydomain.com 时,它应该在 /webapp 中打开应用程序。如果我调用 mydomain.com/admin 它应该打开管理应用程序(这也是一个 angularj2 应用程序)。

到目前为止我尝试过的如下:

server {
  listen       80;
  server_name  localhost;
  root /usr/share/nginx/html/webapp;    
  index index.html index.htm;

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

  location /admin/ {
    alias /usr/share/nginx/html/admin/;
    try_files /admin/$uri /admin/index.html;
  }
}

我希望我能得到帮助。提前致谢!

【问题讨论】:

    标签: angular nginx location html5mode


    【解决方案1】:

    试试:

    root /usr/share/nginx/html/webapp;    
    index index.html index.htm;
    
    location / {
        try_files $uri $uri/ /index.html;
    }
    
    location /admin {
        root /usr/share/nginx/html;
        try_files $uri $uri/ /admin/index.html;
    }
    

    root 指令优于 alias 指令。详情请见this document

    index 指令是继承的,不需要重复。

    $uri/ 术语将鼓励 nginx/ 附加到目录的 URI,以便 index 指令正常工作。我还更正了您的一些 try_files 条款。详情请见this document

    【讨论】:

    • 我尝试从 try_files 中删除 /admin/ 并将其附加到“/admin”位置的根目录中,但这不起作用。你知道为什么吗?
    • /admin/index.html 在哪里?我的回答假设它位于/usr/share/nginx/html/admin/index.html$uri 变量已经包含 /admin/ - 所以它不应该出现在 root 语句中。
    • 是的,它位于那里 文件结构如下所示:/usr/share/nginx/html/ - admin - webapp
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2017-01-19
    相关资源
    最近更新 更多