如何在 Nginx Ingress 上通过 X-Forwarded-For Header 限制访问?

How Can I Restrict Acces By X-Forwarded-For Header on Nginx Ingress?

我有一个 kubernetes 集群和 nginx ingress.Requests 来自 cloudflare 到这个 ingress.Can 我通过 x-forwarded-for header 限制对入口的访问?例如,我的 ip 是 1.1.1.1 和 cloudlare 2.2.2.2。当我通过 cloudflare 发出请求时,cloudflare 将 x-forwarded-for header 设置为:1.1.1.1。如果 x-forwarded-for header 而不是 1.1.1.1

,我想限制对我的入口的访问

我将此注释添加到入口,现在可以使用了!

nginx.ingress.kubernetes.io/server-snippet

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: services-ingress
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
               if ($http_x_forwarded_for != "<my-ip-address>") {
               return 301 https://<redirect-address>;
                    }
spec:
  rules:
  - host: <my-host>
    http:
      paths:
      - path: /<my-path>
        backend:
          serviceName: <my-service>
          servicePort: 80