如何解决使用 K8s 入口部署 Web 应用程序时出现 JS 404 错误
我正在尝试官方示例 Example: Deploying PHP Guestbook application with MongoDB。
一切都很好,但是当我为它部署一个 ingress
时,我可以在 chrome 网络调试中看到下一个(这里,31083 是节点端口):
Request URL: http://10.192.244.109:31083/gb 200 OK
Request URL: http://10.192.244.109:31083/controllers.js 404 Not Found
ingress.yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
Metadata:
name: gb-ingress
annotations:
Nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /gb
pathType: Prefix
backend:
service:
name: frontend
port:
number: 80
apiVersion: v1
kind: Service
Metadata:
annotations:
labels:
helm.sh/chart: ingress-Nginx-3.27.0
app.kubernetes.io/name: ingress-Nginx
app.kubernetes.io/instance: ingress-Nginx
app.kubernetes.io/version: 0.45.0
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-Nginx-controller
namespace: ingress-Nginx
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
- name: https
port: 443
protocol: TCP
targetPort: https
selector:
app.kubernetes.io/name: ingress-Nginx
app.kubernetes.io/instance: ingress-Nginx
app.kubernetes.io/component: controller
我从 chrome 得到的 K8s 留言簿索引页的源代码:
<html ng-app="guestbook">
<head>
<Meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>Guestbook</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<script src="controllers.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.0/ui-bootstrap-tpls.js"></script>
</head>
<body ng-controller="guestbookCtrl">
<div style="width: 50%; margin-left: 20px">
<h2>Guestbook</h2>
<form>
<fieldset>
<input ng-model="msg" placeholder="Messages" class="form-control" type="text" name="input"><br>
<button type="button" class="btn btn-primary" ng-click="controller.onguestbook()">Submit</button>
</fieldset>
</form>
<div>
<div ng-repeat="msg in messages track by $index">
{{msg}}
</div>
</div>
</div>
</body>
</html>
我知道我的 ingress
只是指定了 /gb
前缀,所以 http://10.192.244.109:31083/controllers.js
无法路由到正确的服务,但我该如何使其工作?
我想也许可以为 js
重写添加另一个入口规则,但是如果我有多个应用程序怎么办?有什么建议吗?
解决方法
您应该能够使用正确的 rewrite-rule
和 path
模式执行此操作:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: gb-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- http:
paths:
- path: /gb(/|$)(.*)
pathType: Prefix
backend:
service:
name: frontend
port:
number: 80
这将以下列方式重写您的请求:
- /gb -> /
- /gb/controller.js -> /controller.js
- /gb/foo -> /foo
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。