Kubernetes 上的 Angular(入口路由)

Nic*_*uck 4 kubernetes angular nginx-ingress

我需要如何配置 Angular 7 应用程序正在运行的入口?

\n
apiVersion: extensions/v1beta1\nkind: Ingress\nmetadata:\n  name: myingress\n  annotations:\n    nginx.ingress.kubernetes.io/rewrite-target: /\n    nginx.ingress.kubernetes.io/use-regex: "true"\nspec:\n  rules:\n  - http:\n      paths:\n      - path:\n        backend:\n          serviceName: angular-service\n          servicePort: 80\n      - path: /test\n        backend:\n          serviceName: angular-service\n          servicePort: 80\n
Run Code Online (Sandbox Code Playgroud)\n

Angular 由 nginx 镜像托管:

\n
FROM nginx:alpine\nCOPY . /usr/share/nginx/html\n
Run Code Online (Sandbox Code Playgroud)\n

在 Kubernetes 上:

\n
FROM nginx:alpine\nCOPY . /usr/share/nginx/html\n
Run Code Online (Sandbox Code Playgroud)\n

在路上/一切正常。但在上面/test不起作用。\n控制台输出:

\n
> Uncaught SyntaxError: Unexpected token < runtime.js:1 \n\n> Uncaught SyntaxError: Unexpected token < polyfills.js:1\n\n> Uncaught SyntaxError: Unexpected token < main.js:1\n
Run Code Online (Sandbox Code Playgroud)\n

这就是我在 angular.json 上进行更改的原因:

\n
"baseHref" : "/test",\n
Run Code Online (Sandbox Code Playgroud)\n

但现在我在两个位置都遇到相同的错误。我\xc2\xb4m 做错了什么?

\n

编辑详细信息

\n

入口控制器(版本 0.25.0):

\n

https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

\n

入口服务(对于天蓝色):

\n

https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/cloud-generic.yaml

\n

测试程序:

\n

该应用程序是用构建的

\n
"baseHref" : "",\n
Run Code Online (Sandbox Code Playgroud)\n

当我在服务器上运行应用程序时,一切正常(baseHref 也经过验证)。

\n
$ sudo docker run --name test -p 80:80 test:v1\n
Run Code Online (Sandbox Code Playgroud)\n

在 Kubernetes 上,应用程序在位置上运行/(仅当我使用注释 nginx.ingress.kubernetes.io/rewrite-target: / 时)。\n如果我尝试输入,/test我会得到一个空页面。

\n

日志:

\n

$ sudo kubectl 日志 app-589ff89cfb-9plfs

\n
10.xxx.x.xx - - [11/Jul/2019:18:16:13 +0000] "GET / HTTP/1.1" 200 574 "https://52.xxx.xx.xx/test" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "93.xxx.xxx.xxx"\n
Run Code Online (Sandbox Code Playgroud)\n

$ sudo kubectl 日志 -n ingress-nginx nginx-ingress-controller-6df4d8b446-6rq65

\n

地点/

\n
93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:31 +0000] "GET / HTTP/2.0" 200 279 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 261 0.002 [default-app2-service-80] [] 10.xxx.x.48:80 574 0.004 200 95abb8e14b1dd95976cd44f23a2d829a\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:31 +0000] "GET /runtime.js HTTP/2.0" 200 2565 "https://52.xxx.xx.xx/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 44 0.001 [default-app2-service-80] [] 10.xxx.x.49:80 9088 0.004 200 d0c2396f6955e82824b1dec60d43b4ef\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:31 +0000] "GET /polyfills.js HTTP/2.0" 200 49116 "https://52.xxx.xx.xx/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 25 0.000 [default-app2-service-80] [] 10.xxx.x.48:80 242129 0.000 200 96b5d57f9baf00932818f850abdfecca\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:31 +0000] "GET /styles.js HTTP/2.0" 200 5464 "https://52.xxx.xx.xx/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 23 0.009 [default-app2-service-80] [] 10.xxx.x.49:80 16955 0.008 200 c3a7f1f937227a04c9eec9e1eab107b3\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:31 +0000] "GET /main.js HTTP/2.0" 200 3193 "https://52.xxx.xx.xx/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 21 0.019 [default-app2-service-80] [] 10.xxx.x.49:80 12440 0.016 200 c0e12c3eaec99212444cf916c7d6b27b\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:31 +0000] "GET /runtime.js.map HTTP/2.0" 200 9220 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 24 0.004 [default-app2-service-80] [] 10.xxx.x.48:80 9220 0.008 200 f1a820a384ee9e7a61c74ebb8f3cbf68\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:31 +0000] "GET /vendor.js HTTP/2.0" 200 643193 "https://52.xxx.xx.xx/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 23 0.130 [default-app2-service-80] [] 10.xxx.x.48:80 3391734 0.132 200 1cf47ed0d8a0e470a131dddb22e8fc48\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:31 +0000] "GET /polyfills.js.map HTTP/2.0" 200 241031 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 26 0.001 [default-app2-service-80] [] 10.xxx.x.49:80 241031 0.000 200 75413e809cd9739dc0b9b300826dd107\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:31 +0000] "GET /styles.js.map HTTP/2.0" 200 19626 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 23 0.000 [default-app2-service-80] [] 10.xxx.x.48:80 19626 0.004 200 1aa0865cbf07ffb1753d0a3eb630b4d7\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:32 +0000] "GET /favicon.ico HTTP/2.0" 200 1471 "https://52.xxx.xx.xx/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 55 0.048 [default-app2-service-80] [] 10.xxx.x.49:80 5430 0.000 200 2c015813c697c61f3cc6f67bb3bf7f75\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:32 +0000] "GET /vendor.js.map HTTP/2.0" 200 3493937 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 24 0.377 [default-app2-service-80] [] 10.xxx.x.49:80 3493937 0.380 200 7b41bbbecafc2fb037c934b5509de245\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:55:32 +0000] "GET /main.js.map HTTP/2.0" 200 6410 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 22 0.169 [default-app2-service-80] [] 10.xxx.x.48:80 6410 0.104 200 cc23aa543c19ddc0f55b4a922cc05d04\n
Run Code Online (Sandbox Code Playgroud)\n

地点/test

\n
93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:43:08 +0000] "GET /test/ HTTP/2.0" 200 274 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 294 0.000 [default-test-service-80] [] 10.xxx.x.xx:80 574 0.000 200 2b560857eba8dd1242e359d5eea0a84b\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:43:08 +0000] "GET /test/runtime.js HTTP/2.0" 200 274 "https://52.xxx.xx.xx/test/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 53 0.000 [default-test-service-80] [] 10.xxx.x.49:80 574 0.000 200 2695a85077c64d40a5806fb53e2977e5\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:43:08 +0000] "GET /test/polyfills.js HTTP/2.0" 200 274 "https://52.xxx.xx.xx/test/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 30 0.003 [default-test-service-80] [] 10.xxx.x.48:80 574 0.000 200 8bd4e421ee8f7f9db6b002ac40bf2025\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:43:08 +0000] "GET /test/styles.js HTTP/2.0" 200 274 "https://52.xxx.xx.xx/test/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 28 0.001 [default-test-service-80] [] 10.xxx.x.49:80 574 0.000 200 db7f0cb93b90a41d623552694e5e74b6\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:43:08 +0000] "GET /test/vendor.js HTTP/2.0" 200 274 "https://52.xxx.xx.xx/test/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 28 0.001 [default-test-service-80] [] 10.xxx.x.48:80 574 0.000 200 0e5eb8fc77a6fb94b87e64384ac083e0\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:43:08 +0000] "GET /test/main.js HTTP/2.0" 200 274 "https://52.xxx.xx.xx/test/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 27 0.001 [default-test-service-80] [] 10.xxx.x.49:80 574 0.000 200 408aa3cbfda25f65cb607e1b1ce47566\n93.xxx.xxx.xxx - [93.xxx.xxx.xxx] - - [12/Jul/2019:06:43:08 +0000] "GET /test/favicon.ico HTTP/2.0" 200 274 "https://52.xxx.xx.xx/test/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" 58 0.001 [default-test-service-80] [] 10.xxx.x.48:80 574 0.000 200 d491507ae073de55a480909b4fab0484\n
Run Code Online (Sandbox Code Playgroud)\n

And*_*inn 5

在不知道 nginx-ingress 版本的情况下,这只是一个猜测。

根据https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite-target的文档说:

从版本 0.22.0 开始,使用注释 nginx.ingress.kubernetes.io/rewrite-target 的入口定义不向后兼容以前的版本。在版本 0.22.0 及更高版本中,请求 URI 中需要传递到重写路径的任何子字符串必须在捕获组中显式定义。

这意味着您需要显式传递如下路径:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: angular-service
          servicePort: 80
      - path: /test/(/|$)(.*)
        backend:
          serviceName: angular-service
          servicePort: 80
Run Code Online (Sandbox Code Playgroud)