【发布时间】:2021-05-29 04:26:54
【问题描述】:
在将我的 Angular 应用程序部署到生产环境时,我遇到了一个问题,即我的应用程序无法查看/使用构建时生成的捆绑脚本(运行时、main、css 等)
背景:
我有一个应该运行两个 Angular 应用程序的 URL。
- example.com
- example.com/subroute
应用程序 #1 运行良好,符合预期。
当我转到 URL 时,应用程序 #2 似乎找到了正确的 index.html 文件,但它找不到任何已创建的脚本 (https://example.com/subroute/runtime-es2015.ad3f6d66c5c633672c3c.js)。
还有什么我可以在这里尝试的想法吗?提前谢谢!
更新 #1:
如果我将 kubernetes 入口配置更改为以下内容,我可以访问我的应用,但现在无法访问预期的 /subroute/api 路由。
例如,我可以点击https://example.com/subroute 并按预期加载我的应用程序,但如果我向https://example.com/subroute/api/entity 发出 GET 请求,我会收到 404。
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/proxy-read-timeout: "12h"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: team-webapp-public-ingress
namespace: default
spec:
tls:
- hosts:
- example.com
secretName: example-tls
rules:
- host: example.com
http:
paths:
- path: /subroute(/($|.*))?
backend:
serviceName: example-subroute-frontend-service
servicePort: 80
- host: example.com
http:
paths:
- path: /subroute/api/
backend:
serviceName: API-service
servicePort: 9872
应用#2 Angular 配置
npm run build -- --output-path=./dist/out --configuration production --buildOptimizer=true --base-href '/subroute/' --deployUrl '/subroute/'
应用 #2 Nginx 配置
# Expires map
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/json max;
application/javascript max;
~image/ max;
}
server {
default_type text/html;
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
expires $expires;
gzip on;
}
应用 #2 Kubernetes 配置
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/proxy-read-timeout: "12h"
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/rewrite-target: /$1
name: team-webapp-public-ingress
namespace: default
spec:
tls:
- hosts:
- example.com
secretName: example-tls
rules:
- host: example.com
http:
paths:
- path: /subroute/
backend:
serviceName: example-subroute-frontend-service
servicePort: 80
- host: example.com
http:
paths:
- path: /subroute/api/
backend:
serviceName: API-service
servicePort: 9872
【问题讨论】:
-
添加了更新。能够查看应用程序,只是现在无法正确映射 API 部分。
标签: angular nginx kubernetes deployment production