这是examplenginx.conf中如何屏蔽用户代理:
### make sure your 'if' statement is in the server block.
### case sensitive http user agent blocking ###
if ($http_user_agent ~ (Catall Spider|AcoiRobot) ) {
return 403;
}
### case insensitive http user agent blocking ###
if ($http_user_agent ~* (foo|bar) ) {
return 403;
}
您可以使用以下example 在入口控制器的nginx.conf 中添加一些配置部分。它向 Nginx 配置添加了一个自定义标头,该标头仅适用于该特定 Ingress:
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-configuration-snippet
annotations:
nginx.ingress.kubernetes.io/configuration-snippet: |
more_set_headers "Request-Id: $req_id";
spec:
rules:
- host: custom.configuration.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /
如果您需要向入口控制器添加一些全局设置,请参阅以下examples:
---
apiVersion: v1
data:
proxy-set-headers: "ingress-nginx/custom-headers"
kind: ConfigMap
metadata:
name: nginx-configuration
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
apiVersion: v1
data:
X-Different-Name: "true"
X-Request-Start: t=${msec}
X-Using-Nginx-Controller: "true"
kind: ConfigMap
metadata:
name: custom-headers
namespace: ingress-nginx
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-configuration
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
data:
proxy-connect-timeout: "10"
proxy-read-timeout: "120"
proxy-send-timeout: "120"
使用前面的示例创建一个 YAML 文件并将其应用到您的集群:
kubectl apply -f nginx-ingress-config.yaml
您可以使用以下命令检查设置是否应用于入口控制器中的 nginx.conf:
# Replace name of the ingress controller with the real name of the pod in the right namespace
$ kubectl exec nginx-ingress-controller-6bdddddb-6dmnw -n kube-system cat /etc/nginx/nginx.conf
# You can find the real name of your ingress controller and namespace using the next command
$ kubectl get pods --all-namespaces | grep nginx-ingress-controller
如果没有关于图表的信息,很难猜测应该设置哪个参数。
如果您使用 helm 存储库中的图表,您可以通过运行来获取它的内容
$ helm fetch <chart/name>
之后,您将在当前目录中获得一个压缩的图表文件。
您可能需要通过读取图表模板目录中的模板文件来为您的代码 sn-p 找到正确的值。
如果您使用的是您编写的图表,您可以使用stable/nginx-ingress 图表作为参考。它有很多配置选项。
更新:
从 0.20.0 版开始,新功能是introduced:
#2997 提供阻止 IP、用户代理和引用者的可能性
全球
参数的用法在Map section of the manual中解释。
如果您需要使用示例,可以在test-case 中找到。
It("should block User-Agents defined in the ConfigMap", func() {
err := f.UpdateNginxConfigMapData("block-user-agents", "~*chrome\\/68\\.0\\.3440\\.106\\ safari\\/537\\.36,AlphaBot")
Expect(err).NotTo(HaveOccurred())
...
// Should be blocked
resp, _, errs := gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36").
End()
Expect(errs).To(BeNil())
Expect(resp.StatusCode).Should(Equal(http.StatusForbidden))
resp, _, errs = gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
Set("User-Agent", "AlphaBot").
End()
Expect(errs).To(BeNil())
Expect(resp.StatusCode).Should(Equal(http.StatusForbidden))
// Shouldn't be blocked
resp, _, errs = gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
Set("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1").
End()
Expect(errs).To(BeNil())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
})