Search code examples
nginxkuberneteskubernetes-ingressnginx-reverse-proxynginx-ingress

Adding Headers to NGINX Ingress using ConfigMap not working


I'm trying to pass my client IP address through my NGINX Ingress using Kubernetes on Azure

I've created this configmap for the NGINX config to add the headers:

apiVersion: v1
data:
  X-Real-IP: $remote_addr;
  X-Forwarded-For: $proxy_add_x_forwarded_for;
  X-Forwarded-Proto: $proxy_x_forwarded_proto;
  use-forwarded-headers: "true"
  use-proxy-protocol: "true"
  real-ip-header: "proxy_protocol"
kind: ConfigMap
metadata:
  name: custom-headers
  namespace: default

Then added this config to reference the previous file:

apiVersion: v1
data:
  proxy-set-headers: "custom-headers"
  externalTrafficPolicy: Local
kind: ConfigMap
metadata:
  name: ingress-nginx-controller
  namespace: default
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

If I describe my nginx controller:

kubectl describe deploy ingress-nginx-controller

I can see the line:

--configmap=$(POD_NAMESPACE)/ingress-nginx-controller

If I describe the ingress-nginx-controller configmap

kubectl describe configmap ingress-nginx-controller

I can see the following in the data section:

proxy-set-headers:
----
custom-headers

If I log out the nginx.conf file in my controller though, I can't see the changed values. For example this is still the default:

proxy_set_header X-Forwarded-For        $remote_addr;

Solution

  • You are missing the namespace prefix for proxy-set-headers value. Because you have deployed custom-headers configmap to the default namespace, it should be

    data:
      proxy-set-headers: default/custom-headers