Nginx 入口控制器重写目标注释和规则以将尾部斜杠添加到 url
Nginx ingress controller rewrite-target annotation and rule to add a trailing slash to url
我正在尝试将静态网站部署到使用官方 Nginx Ingress 控制器的 Kubernetes 集群。网站的文件夹结构看起来有点像这样:
/
├── about
│ └── index.html
├── casestudy
│ ├── data-and-analytics
│ │ └── index.html
│ └── workflow-automation
│ └── index.html
├── contact
│ └── index.html
├── css
│ ├── font-awesome.min.css
│ ├── fonts
│ │ ├── slick.eot
│ │ ├── slick.svg
│ │ ├── slick.ttf
│ │ └── slick.woff
│ ├── footer.css
│ ├── layout.css
...
我的入口定义如下所示:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: website-ingress
annotations:
kubernetes.io/ingress.class: nginx
# nginx.ingress.kubernetes.io/rewrite-target: /
cert-manager.io/cluster-issuer: letsencrypt
spec:
tls:
- hosts:
- website.com
secretName: website-tls
rules:
- host: website.com
http:
paths:
- path: /
backend:
serviceName: website-svc
servicePort: 8080
这在大多数情况下工作正常,除了如果我忘记在 URL 的末尾放置斜杠,例如 https://website.com/about
,我将被路由到错误页面。我明白为什么会这样——Nginx 正在寻找 about.html
文件,但找不到。但我不知道如何解决这个问题。
我最想做的是为没有请求的请求添加一个尾随 /
。但我也不想在浏览器请求 css 文件时执行此操作。
为此我应该使用什么重定向注释和规则?
谢谢。
试试这个:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: website-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: //
spec:
rules:
- http:
paths:
- path: /(.*)
backend:
serviceName: website-svc
servicePort: 8080
它非常适合我。 运行之后:
curl <ingress ip>/about
您可以在日志中看到以下内容:
入口控制器
kubectl logs -n ingress-nginx ingress-nginx-controller-6dc856845b-rf2ft
<ip address> - - [29/Dec/2020:18:40:13 +0000] "GET /about HTTP/1.1" 200 7 "-" "curl/7.64.0" 83 0.000 [default-nginx-deployment-80] [] 10.0.1.8:80 7 0.000 200 (...)
为网站提供服务的 nginx pod
kubectl logs nginx-deployment-6c8c7fdf65-w4h4b
10.0.1.10 - - [29/Dec/2020:18:40:13 +0000] "GET /about/ HTTP/1.1" 200 7 "-" "curl/7.64.0" "<ip address>"
最终解决这种情况的是这样的片段:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: website-ingress
annotations:
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt
nginx.ingress.kubernetes.io/rewrite-target: /
# Rewrite all URLs not ending with a segment containing . or ? with a trailing slash
# So basically we are rewriting all folder names with a trailing slash.
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^([^.?]*[^/])$ / redirect;
spec:
tls:
- hosts:
- website.com
secretName: website-tls
rules:
- host: website.com
http:
paths:
- path: /
backend:
serviceName: website-svc
servicePort: 8080
这将使我们重写所有以不包含 .
(句点 - 从而避免文件名) 和 ?
(问号 - 从而避免所有查询字符串) 带有尾部斜杠。这适用于我的情况。
我正在尝试将静态网站部署到使用官方 Nginx Ingress 控制器的 Kubernetes 集群。网站的文件夹结构看起来有点像这样:
/
├── about
│ └── index.html
├── casestudy
│ ├── data-and-analytics
│ │ └── index.html
│ └── workflow-automation
│ └── index.html
├── contact
│ └── index.html
├── css
│ ├── font-awesome.min.css
│ ├── fonts
│ │ ├── slick.eot
│ │ ├── slick.svg
│ │ ├── slick.ttf
│ │ └── slick.woff
│ ├── footer.css
│ ├── layout.css
...
我的入口定义如下所示:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: website-ingress
annotations:
kubernetes.io/ingress.class: nginx
# nginx.ingress.kubernetes.io/rewrite-target: /
cert-manager.io/cluster-issuer: letsencrypt
spec:
tls:
- hosts:
- website.com
secretName: website-tls
rules:
- host: website.com
http:
paths:
- path: /
backend:
serviceName: website-svc
servicePort: 8080
这在大多数情况下工作正常,除了如果我忘记在 URL 的末尾放置斜杠,例如 https://website.com/about
,我将被路由到错误页面。我明白为什么会这样——Nginx 正在寻找 about.html
文件,但找不到。但我不知道如何解决这个问题。
我最想做的是为没有请求的请求添加一个尾随 /
。但我也不想在浏览器请求 css 文件时执行此操作。
为此我应该使用什么重定向注释和规则?
谢谢。
试试这个:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: website-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: //
spec:
rules:
- http:
paths:
- path: /(.*)
backend:
serviceName: website-svc
servicePort: 8080
它非常适合我。 运行之后:
curl <ingress ip>/about
您可以在日志中看到以下内容:
入口控制器
kubectl logs -n ingress-nginx ingress-nginx-controller-6dc856845b-rf2ft
<ip address> - - [29/Dec/2020:18:40:13 +0000] "GET /about HTTP/1.1" 200 7 "-" "curl/7.64.0" 83 0.000 [default-nginx-deployment-80] [] 10.0.1.8:80 7 0.000 200 (...)
为网站提供服务的 nginx pod
kubectl logs nginx-deployment-6c8c7fdf65-w4h4b
10.0.1.10 - - [29/Dec/2020:18:40:13 +0000] "GET /about/ HTTP/1.1" 200 7 "-" "curl/7.64.0" "<ip address>"
最终解决这种情况的是这样的片段:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: website-ingress
annotations:
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt
nginx.ingress.kubernetes.io/rewrite-target: /
# Rewrite all URLs not ending with a segment containing . or ? with a trailing slash
# So basically we are rewriting all folder names with a trailing slash.
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^([^.?]*[^/])$ / redirect;
spec:
tls:
- hosts:
- website.com
secretName: website-tls
rules:
- host: website.com
http:
paths:
- path: /
backend:
serviceName: website-svc
servicePort: 8080
这将使我们重写所有以不包含 .
(句点 - 从而避免文件名) 和 ?
(问号 - 从而避免所有查询字符串) 带有尾部斜杠。这适用于我的情况。