我正在尝试使用Kubernetes托管我自己的Nextcloud服务器。
我希望我的Nextcloud服务器从http://localhost:32738/nextcloud
访问,但每次我访问该URL,它被重定向到http://localhost:32738/login
并给我404 Not Found
。
如果我将路径替换为:
path: /
然后,它在http://localhost:32738/login
上没有问题,但正如我所说,这不是我要寻找的解决方案。登录页面应该从http://localhost:32738/nextcloud/login
访问。
转到http://127.0.0.1:32738/nextcloud/
在初始设置中确实有效,但在此之后它变得不可访问,因为它总是重定向到:
http://127.0.0.1:32738/apps/dashboard/
和not to:
http://127.0.0.1:32738/nextcloud/apps/dashboard/
这是我的网址:
#Nextcloud-Dep
apiVersion: apps/v1
kind: Deployment
metadata:
name: nextcloud-server
labels:
app: nextcloud
spec:
replicas: 1
selector:
matchLabels:
pod-label: nextcloud-server-pod
template:
metadata:
labels:
pod-label: nextcloud-server-pod
spec:
containers:
- name: nextcloud
image: nextcloud:22.2.0-apache
env:
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: nextcloud
key: db-name
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: nextcloud
key: db-username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: nextcloud
key: db-password
- name: POSTGRES_HOST
value: nextcloud-database:5432
volumeMounts:
- name: server-storage
mountPath: /var/www/html
subPath: server-data
volumes:
- name: server-storage
persistentVolumeClaim:
claimName: nextcloud
---
#Nextcloud-Serv
apiVersion: v1
kind: Service
metadata:
name: nextcloud-server
labels:
app: nextcloud
spec:
selector:
pod-label: nextcloud-server-pod
ports:
- port: 80
protocol: TCP
name: nextcloud-server
---
#Database-Dep
apiVersion: apps/v1
kind: Deployment
metadata:
name: nextcloud-database
labels:
app: nextcloud
spec:
replicas: 1
selector:
matchLabels:
pod-label: nextcloud-database-pod
template:
metadata:
labels:
pod-label: nextcloud-database-pod
spec:
containers:
- name: postgresql
image: postgres:13.4
env:
- name: POSTGRES_DATABASE
valueFrom:
secretKeyRef:
name: nextcloud
key: db-name
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: nextcloud
key: db-username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: nextcloud
key: db-password
- name: POSTGRES_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: nextcloud
key: db-rootpassword
- name: PGDATA
value: /var/lib/postgresql/data/
volumeMounts:
- name: database-storage
mountPath: /var/lib/postgresql/data/
subPath: data
volumes:
- name: database-storage
persistentVolumeClaim:
claimName: nextcloud
---
#Database-Serv
apiVersion: v1
kind: Service
metadata:
name: nextcloud-database
labels:
app: nextcloud
spec:
selector:
pod-label: nextcloud-database-pod
ports:
- port: 5432
protocol: TCP
name: nextcloud-database
---
#PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: nextcloud-pv
labels:
type: local
spec:
capacity:
storage: 8Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/tmp"
---
#PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nextcloud
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 8Gi
---
#Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nextcloud-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- http:
paths:
- backend:
service:
name: nextcloud-server
port:
number: 80
pathType: Prefix
path: /nextcloud(/.*)
---
#Secret
apiVersion: v1
kind: Secret
metadata:
name: nextcloud
labels:
app: nextcloud
immutable: true
stringData:
db-name: nextcloud
db-username: nextcloud
db-password: changeme
db-rootpassword: longpassword
username: admin
password: changeme
ingress-nginx被安装为:
helm install nginx ingress-nginx/ingress-nginx
如果您需要我提供更多的信息,请告诉我。
在您的示例中,后端服务中公开的URL与Ingress规则中指定的路径之间存在差异。这就是为什么你得到一个错误。
为了避免这种情况,你可以使用重写规则。
使用该值,您的入口路径将被重写为您提供的值。此注释ingress.kubernetes.io/rewrite-target: /login
将在向后端服务发送请求之前将URL/nextcloud/login
重写为/login
。
但:
从0.22.0版本开始,入口定义使用注释nginx.ingress.kubernetes。Io/rewrite-target与以前的版本不向后兼容。
在本文档中您可以找到以下示例:
$ echo '
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /something(/|$)(.*)
' | kubectl create -f -
在此入口定义中,
(.*)
捕获的任何字符都将分配给占位符$2
,然后将其用作rewrite-target
注释中的参数。
所以在你的URL中,你可以看到想要的/nextcloud/login
,但重写将导致在入口规则中更改/login
的路径并找到你的后端。我建议使用以下选项之一:
path: /nextcloud(/.*)
nginx.ingress.kubernetes.io/rewrite-target: /$1
或
path: /nextcloud/login
nginx.ingress.kubernetes.io/rewrite-target: /login
请参阅本文。