访问由 nginx-ingress 处理的 webapp 的子域
Accessing subdomains of webapp handled by nginx-ingress
我有两个服务运行:
$kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 49m
shiny NodePort 10.110.49.57 <none> 3838:30240/TCP 34m
web NodePort 10.98.56.71 <none> 80:31758/TCP 39m
以及以下入口:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: web
servicePort: 80
- path: /shiny/*
backend:
serviceName: shiny
servicePort: 3838
如果我直接通过 minikube service shiny
:
访问它,我会从 shiny
获得我想要的行为
应用入口后,子域停止工作:
为什么会这样?
我想访问 shiny
中的内容,从 web
到 url。
例如。 <iframe src=.../shiny/test1></iframe>
.
Nginx 使用正则表达式匹配您的 paths/locations 请求。尝试更改:
/shiny/* to /shiny/.*
以下 yaml 调整解决了问题:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: web
servicePort: 80
- path: /shiny(/|$)(.*)
backend:
serviceName: shiny
servicePort: 3838
来源:https://kubernetes.github.io/ingress-nginx/examples/rewrite/
编辑:
以上导致了更多的麻烦。最后,我使用了以下入口配置:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite / break;
rewrite /shiny/(.*) / break;
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: web
servicePort: 80
- path: /shiny
backend:
serviceName: shiny
servicePort: 3838
我有两个服务运行:
$kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 49m
shiny NodePort 10.110.49.57 <none> 3838:30240/TCP 34m
web NodePort 10.98.56.71 <none> 80:31758/TCP 39m
以及以下入口:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: web
servicePort: 80
- path: /shiny/*
backend:
serviceName: shiny
servicePort: 3838
如果我直接通过 minikube service shiny
:
shiny
获得我想要的行为
应用入口后,子域停止工作:
为什么会这样?
我想访问 shiny
中的内容,从 web
到 url。
例如。 <iframe src=.../shiny/test1></iframe>
.
Nginx 使用正则表达式匹配您的 paths/locations 请求。尝试更改:
/shiny/* to /shiny/.*
以下 yaml 调整解决了问题:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: web
servicePort: 80
- path: /shiny(/|$)(.*)
backend:
serviceName: shiny
servicePort: 3838
来源:https://kubernetes.github.io/ingress-nginx/examples/rewrite/
编辑:
以上导致了更多的麻烦。最后,我使用了以下入口配置:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite / break;
rewrite /shiny/(.*) / break;
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: web
servicePort: 80
- path: /shiny
backend:
serviceName: shiny
servicePort: 3838