带有自定义 --base-href 和 Nginx 路由的 Angular SPA

use*_*398 9 nginx angular

我们有一个 Angular SPA 应用程序,需要更改其基本路径

www.site.comwww.site.com/app

我们添加--base-href=/app到 ng build 命令中

RUN ng build --output-path=dist --base-href=/app/ --prod
Run Code Online (Sandbox Code Playgroud)

我们的 nginx 配置中有这样的内容:

location / {
 try_files $uri $uri/ /index.html;
}
Run Code Online (Sandbox Code Playgroud)

我们把它改为

location /app/ {
 root /usr/share/nginx/html/app;
 try_files $uri $uri/ /app/index.html;
}
Run Code Online (Sandbox Code Playgroud)

访问该网站localhost/consumerhub会抛出 500 错误。这是我在日志中看到的:

2019/10/15 19:53:51 [error] 7#7: *1 rewrite or internal redirection cycle while internally redirecting to "/app/index.html", client: 172.17.0.1, server: , request: "GET /app/ HTTP/1.1", host: "localhost:9182"
172.17.0.1 - - [15/Oct/2019:19:53:51 +0000] "GET /app/ HTTP/1.1" 500 572 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"
Run Code Online (Sandbox Code Playgroud)

应该是什么?提前致谢!

小智 2

使用ng build --prod --base-href /app/ --deploy-url /app/(更改为应用程序运行的端口号(80))

  location /app/ {
      rewrite ^/app/(.*) /$1 break;
      proxy_pass         http://localhost:80/app/;
      proxy_set_header   Host $http_host;
      proxy_set_header   X-Real-IP $remote_addr;
  }
Run Code Online (Sandbox Code Playgroud)