需求实现:
http://www.example.com/login ---> https://www.xyz.com:8443/login/login.httml
http://www.example.com/login/ ---> https://www.xyz.com:8443/login/login.httml
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: xxxxxx
nginx.ingress.kubernetes.io/configuration-snippet: |
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header Access-Control-Allow-Credentials false;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
nginx.ingress.kubernetes.io/permanent-redirect: https://www.xyz.com:8443/login/login.httml
creationTimestamp: "2023-08-24T07:56:43Z"
generation: 2
name: xxxxxx
namespace: test
resourceVersion: "114549163"
uid: 375380c2-2f5f-47bf-a0d7-1fbebaed5b63
spec:
rules:
- host: www.example.com
http:
paths:
- backend:
service:
name: microservice
port:
name: http
path: /login
pathType: Prefix
- backend:
service:
name: microservice
port:
name: http
path: /login/
pathType: Prefix
这段 Ingress 配置定义了一个入口资源,用于将外部请求路由到 Kubernetes 集群内部的服务。
主要元素包括:
- metadata:包含一些注释和元数据,例如 ingress 类、SSL 重定向、CORS 头等。
-
spec:指定了路由规则,其中
host
为www.example.com
,表示针对该域名的请求。 -
http.paths:定义了两个路径(
/login
和/login/
),都指向名为microservice
的服务,使用 HTTP 协议。
最终,这个 Ingress 配置将特定路径的请求重定向到相应的服务,支持 CORS,并强制 SSL 重定向。
配置中的 nginx.ingress.kubernetes.io/permanent-redirect
注释指定了一个永久重定向。
具体来说,它会将所有访问 www.example.com
的请求重定向到指定的 URL,即 https://www.xyz.com:8443/login/login.httml
。
这种重定向使用 HTTP 301 状态码,意味着搜索引擎和浏览器会将该 URL 视为资源的永久位置。这样可以有效地引导用户或客户端到新的地址进行认证或登录。同时,结合 force-ssl-redirect
注释,确保所有请求都使用 HTTPS,从而增强安全性。
这个 Ingress 配置中,具体访问什么会重定向到哪里:
访问示例和重定向行为
-
直接访问主机:
-
请求:用户在浏览器中输入
http://www.example.com/login
或http://www.example.com/login/
。 -
行为:由于配置了
nginx.ingress.kubernetes.io/force-ssl-redirect
,这个 HTTP 请求会被强制重定向到 HTTPS:-
重定向到:
https://www.example.com/login
或https://www.example.com/login/
-
重定向到:
-
请求:用户在浏览器中输入
-
访问 /login 路径:
-
请求:用户在浏览器中输入
https://www.example.com/login
。 -
行为:由于配置了
nginx.ingress.kubernetes.io/permanent-redirect
,这会导致进一步的重定向:-
重定向到:
https://www.xyz.com:8443/login/login.httml
-
重定向到:
- 这意味着用户实际上会被引导到身份验证服务的授权页面。
-
请求:用户在浏览器中输入
-
访问 /login/ 路径:
-
请求:用户在浏览器中输入
https://www.example.com/login/
。 -
行为:同样会根据
nginx.ingress.kubernetes.io/permanent-redirect
进行重定向:-
重定向到:同样是
https://www.xyz.com:8443/login/login.httml
-
重定向到:同样是
-
请求:用户在浏览器中输入
总结
- 所有访问
http://www.example.com/login
或http://www.example.com/login/
的请求都会首先被重定向到 HTTPS,然后再次重定向到身份验证服务。 - 这种配置主要用于实现 OAuth2.0 的认证流程,将用户从入口应用引导到身份验证系统。
网友评论