在 nginx 服务器中为 Angular 5 应用程序配置子文件夹

Ahe*_*elp 5 nginx amazon-ec2 amazon-web-services angular

我有两个角度应用程序发布到两个不同的子文件夹“testapp1”和“testapp2”。

url's - 
    https://dev.testurl.org/foo -- For testapp1
    https://dev.testurl.org/bar -- For testapp2
Run Code Online (Sandbox Code Playgroud)

所以我用basehref构建角度

For testapp1:
 ng build --prod --base-href /foo/ 

For testapp2:
  ng build --prod --base-href /bar/
Run Code Online (Sandbox Code Playgroud)

我的 nginx 配置,

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  client_max_body_size 1000M;
  server_name dev.testurl.org localhost;

  location /testapp1/ {
            autoindex on;
    root /testapp1/dist;
    try_files $uri $uri/ /foo/index.html;    
  }
  location /testapp2/ {
            autoindex on;
    root /testapp2/dist;
    try_files $uri $uri/ /bar/index.html;    
  }
Run Code Online (Sandbox Code Playgroud)

当我尝试点击https://dev.testurl.org/foo或 /bar 时,它有控制台错误,显示“意外语法错误”,基本上它没有加载正确的源。任何想法?我是否正确构建应用程序或做了错误的 nginx 配置?

Ric*_*ith 4

您的配置有两个问题:

root当可以通过连接文档根与请求的 URI 来计算文件路径时,使用该指令。否则,请使用alias. 有关详细信息,请参阅此文档

location指令用于匹配全部或部分 URI。有关详细信息,请参阅此文档

例如:

location /foo {
    autoindex on;
    alias /testapp1/dist;
    try_files $uri $uri/ /foo/index.html;    
}
location /bar {
    autoindex on;
    alias /testapp2/dist;
    try_files $uri $uri/ /bar/index.html;    
}
Run Code Online (Sandbox Code Playgroud)

和值应该要么都以 结尾,要么都不以 结尾,location以获得正确的字符串替换。alias//