<p>I have a nodejs application running on Google Kubernetes Engine (v1.20.8-gke.900)</p>
<p>I want to add custom header to get client's Region and lat long so I refer to <a href="https://cloud.google.com/kubernetes-engine/docs/concepts/ingress" rel="nofollow noreferrer">this article</a> and <a href="https://cloud.google.com/load-balancing/docs/https/custom-headers" rel="nofollow noreferrer">this one</a> also and created below kubernetes config file, but when I am printing the header I am not getting any custom header.</p>
<pre class="lang-yaml prettyprint-override"><code>#k8s.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: my-app-ns-prod
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: npm-app-deployment
  namespace: my-app-ns-prod
  labels:
    app: npm-app-deployment
    tier: backend

spec:
  template:
    metadata:
      name: npm-app-pod
      namespace: my-app-ns-prod
      labels:
        app: npm-app-pod
        tier: backend

    spec:
      containers:
      - name: my-app-container
        image: us.gcr.io/img/my-app:latest
        ports:
        - containerPort: 3000
          protocol: TCP
        envFrom:
          - secretRef:
              name: npm-app-secret
          - configMapRef:
              name: npm-app-configmap
        imagePullPolicy: Always
      imagePullSecrets:
        - name: gcr-regcred

  replicas: 3
  minReadySeconds: 30

  selector:
    matchLabels:
      app: npm-app-pod
      tier: backend
---
apiVersion: v1
kind: Service
metadata:
  name: npm-app-service
  namespace: my-app-ns-prod
  annotations:
    cloud.google.com/backend-config: '{&quot;ports&quot;: {&quot;80&quot;:&quot;npm-app-backendconfig&quot;}}'
    cloud.google.com/neg: '{&quot;ingress&quot;: true}'
spec:
  selector:
    app: npm-app-pod
    tier: backend
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 3000
    - name: https
      protocol: TCP
      port: 443
      targetPort: 3000
  type: LoadBalancer
---
apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: npm-app-backendconfig
  namespace: my-app-ns-prod
spec:
  customRequestHeaders:
    headers:
    - &quot;HTTP-X-Client-CityLatLong:{client_city_lat_long}&quot;
    - &quot;HTTP-X-Client-Region:{client_region}&quot;
    - &quot;HTTP-X-Client-Region-SubDivision:{client_region_subdivision}&quot;
    - &quot;HTTP-X-Client-City:{client_city}&quot;
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
spec:
  rules:
    - host: app.example.com
      http:
        paths:
          - path: /api/v1
            pathType: Prefix
            backend:
              service:
                name: npm-app-service
                port:
                  number: 80
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: npm-app-configmap
  namespace: my-app-ns-prod
data:
  APP_ID: &quot;My App&quot;
  PORT: &quot;3000&quot;
---
apiVersion: v1
kind: Secret
metadata:
  name: npm-app-secret
  namespace: my-app-ns-prod
type: Opaque
data:
  MONGO_CONNECTION_URI: &quot;&quot;
  SESSION_SECRET: &quot;&quot;
</code></pre>

Raunak Gupta
<p>Actually the issue was with Ingress Controller, I missed to defined &quot;<code>cloud.google.com/backend-config</code>&quot;. Once I had defined that I was able to get the custom header. Also I switched to <strong>GKE Ingress controller</strong> (<code>gce</code>) from <code>nginx</code>. But the same things works with <code>nginx</code> also</p>
<p>This is how my final ingress looks like.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    cloud.google.com/backend-config: '{&quot;default&quot;: &quot;npm-app-backendconfig&quot;}'
    kubernetes.io/ingress.class: &quot;gce&quot;
spec:
  ...
  ...
</code></pre>
<p>Reference: <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/ingress-features#request_headers" rel="nofollow noreferrer">User-defined request headers</a> :</p>
