Ben Gannaway
Ben Gannaway

Reputation: 1083

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;

Upvotes: 2

Views: 6140

Answers (2)

Mikołaj Głodziak
Mikołaj Głodziak

Reputation: 5267

I have run some tests and the answer provided by the user Yadhu should be correct.

Problem explanation: You are using proxy-set-headers configuration option for ConfigMaps. It is well described here:

Sets custom headers from named configmap before sending traffic to backends. The value format is namespace/name. See example

In this example you can find configuration of the nginx ingress controller via a ConfigMap to pass a custom list of headers to the upstream server.

Look at the example yaml:

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

Problem soultion: You need to use proper format in this option:

proxy-set-headers: "<your namespace>/custom-headers"

Upvotes: 1

Yadhu
Yadhu

Reputation: 46

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

Upvotes: 3

Related Questions