[英]Angular SPA with Custom --base-href and Nginx Routing
We have an angular SPA application that we need to change the base path from我们有一个 angular SPA 应用程序,我们需要更改其基本路径
www.site.com to www.site.com/app www.site.com 到 www.site.com/app
We added --base-href=/app to our ng build command我们在 ng build 命令中添加了 --base-href=/app
RUN ng build --output-path=dist --base-href=/app/ --prod运行 ng build --output-path=dist --base-href=/app/ --prod
We have this in our nginx configuration:我们的 nginx 配置中有这个:
location / {
try_files $uri $uri/ /index.html;
}
We changed it to我们把它改成
location /app/ {
root /usr/share/nginx/html/app;
try_files $uri $uri/ /app/index.html;
}
and visiting the site localhost/consumerhub throws a 500 error.并访问站点 localhost/consumerhub 会引发 500 错误。 This is what I see in the logs:
这是我在日志中看到的:
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" "-" 2019/10/15 19:53:51 [错误] 7#7:*1 重写或内部重定向周期,同时内部重定向到“/app/index.html”,客户端:172.17.0.1,服务器:,请求:“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" "-"
What should it be?应该是什么? Thanks in advance!
提前致谢!
Use ng build --prod --base-href /app/ --deploy-url /app/
(change to your port number where the app runs(80))使用
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;
}
Actually you can add these parameters (--baseHref and --deployURL) in the angular.json configuration, for any environment or for all of them.实际上,您可以在 angular.json 配置中为任何环境或所有环境添加这些参数(--baseHref 和 --deployURL)。
Solution without proxy_pass, just serving the compiled version:没有 proxy_pass 的解决方案,只提供编译版本:
location ^~ /app {
alias /var/www/app-frontend/current;
index index.html;
try_files $uri $uri/ /index.html =404;
}
The important value is =404
, otherwise the Angular subroutes get routed to some mysterios nginx spaces, instead to be returned to the Angular router.重要的值是
=404
,否则 Angular 子路由会被路由到一些神秘的 nginx 空间,而不是返回到 Angular 路由器。
Make sure to have the base tag set to <base href="/app/"></base>
and in angular.json
"baseHref": "/app/",
in architect/configurations/production
确保将基本标签设置为
<base href="/app/"></base>
并在angular.json
"baseHref": "/app/",
in architect/configurations/production
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.