gpt4 book ai didi

nginx - 阻止 ingress-nginx 负载均衡器上的特定路径

转载 作者:行者123 更新时间:2023-12-05 02:03:30 28 4
gpt4 key购买 nike

我有很多域指向 Ingress Controller IP。我想阻止所有域/站点的/特定路径。有没有办法做到这一点。我可以使用 nginx.ingress.kubernetes.io/configuration-snippet: | 每个网站。但是正在寻找一次对所有站点/域/Ingress 资源执行操作的方法。

使用的 Controller :https://kubernetes.github.io/ingress-nginx/

最佳答案

有两种方法可以实现:

<强>1。第一个是使用 server-snippet 注释:

Using the annotation nginx.ingress.kubernetes.io/server-snippet itis possible to add custom configuration in the server configurationblock.

这是我的入口对象 list :

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/server-snippet: |
location ~* /admin-access {
deny all;
return 403;
}
spec:
rules:
- host: domain.com
http:
paths:
- path: /
backend:
serviceName: web
servicePort: 80

请注意使用this方法:

This annotation can be used only once per host.


<强>2。第二个是使用 ConfigMapsServer-snippet :

您要做的就是找到您的configMap:

 kubectl get pod <nginx-ingress-controller>  -o yaml

这位于容器 args 中:

  spec:
containers:
- args:
- /nginx-ingress-controller
- configmap=$(POD_NAMESPACE)/nginx-loadbalancer-conf

然后只需编辑它并添加 server-snippet 部分:

   apiVersion: v1 
data:
server-snippet: |
location /admin-access {
deny all;
}

此方法允许您为 Ingress 资源中定义的所有主机全局定义受限位置。


请注意,使用 server-snippet 时,您阻止的路径无法在入口资源对象中定义。然而,还有另一种通过 ConfigMap 使用 location-snippet 的方法:

location ~* "^/web/admin { 
deny all;
}

对于入口对象中的每个现有路径,都会有入口规则,但它会被特定的 uri 阻止(在上面的示例中,当 admin 出现在 web 之后时,它会被阻止)。所有其他 uri 都将通过。


<强>3。这是一个测试:

➜  curl -H "Host: domain.com"  172.17.0.4/test             
...
"path": "/test",
"headers": {
...
},
"method": "GET",
"body": "",
"fresh": false,
"hostname": "domain.com",
"ip": "172.17.0.1",
"ips": [
"172.17.0.1"
],
"protocol": "http",
"query": {},
"subdomains": [],
"xhr": false,
"os": {
"hostname": "web-6b686fdc7d-4pxt9"
...

这是一个路径被拒绝的测试:

➜  curl -H "Host: domain.com"  172.17.0.4/admin-access

<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.19.0</center>
</body>
</html>

➜ curl -H "Host: domain.com" 172.17.0.4/admin-access/test
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.19.0</center>
</body>
</html>

附加信息:Deprecated APIs Removed In 1.16 .这是您需要知道的:

The v1.22 release will stop serving the following deprecated APIversions in favor of newer and more stable API versions:

Ingress in the extensions/v1beta1 API version will no longer beserved

关于nginx - 阻止 ingress-nginx 负载均衡器上的特定路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65147557/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com