<p>I have a set of IPs <code>(Example: 1.1.1.1/24)</code> and the requests coming from those source IPs <code>(1.1.1.1/24)</code> must redirect to a different URL. I tried the below method.</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-test
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
        if ($http_x_forwarded_for != &quot;1.1.1.1/24&quot;) {
        return 301 https://blablabla;
        }
spec:
  rules:
  - host: my-url
    http:
      paths:
      - path: /some-path-here
        backend:
          serviceName: some-service
          servicePort: 80
</code></pre>
<p>So when I do a curl <code>https://my-url</code> from one of the VMs inside 1.1.1.1/24 network it gives the below response which is correct.</p>
<pre><code>curl -I https://my-url
HTTP/2 301 
date: Tue, 01 Aug 2023 06:34:31 GMT
content-type: text/html
content-length: 162
location: https://blablabla
</code></pre>
<p>But the issue is it redirects to <code>https://blablabla</code> from everywhere when I curl <code>https://my-url</code> not only from <code>1.1.1.1/24</code></p>

Container-Man
<p>I am not sure you'd be able to do this natively with the Kubernetes resources.</p>
<p>You would be able to do this by setting up a proxy (httpd/apache/nginx) as a service the ingress talks to so it goes:</p>
<pre><code>internet -&gt; loadbalancer -&gt; ingress/ingress controller -&gt; proxy -&gt; backend service
</code></pre>
<p>Then at the proxy you can do the redirect to bounce requests to the URL somewhere else upon any criteria you set.</p>
<p>You <em>may</em> also be able to do this with something like a CDN in front of your service such as CloudFront or Google Cloud CDN, but YMMV. I know you can definitely do this with CDN services like Akamai, and probably with Fastly also.</p>
