Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Ingress NGINX removes my response headers

I have ASP.NET WebApi project, which return security headers on each request:

enter image description here

When I run this app in Kubernetes cluster with Ingress NGINX, my headers are missed

enter image description here

How I can configure NGINX to use response headers from my application? Why Ingress NGINX removes my response headers?

I don't have any experience with NGINX configuration. Please suggest how to do that in k8s cluster. Thanks

like image 569
Я TChebur Avatar asked May 09 '20 21:05

Я TChebur


People also ask

Does ingress reverse proxy?

An ingress controller acts as a reverse proxy and load balancer. It implements a Kubernetes Ingress. The ingress controller adds a layer of abstraction to traffic routing, accepting traffic from outside the Kubernetes platform and load balancing it to Pods running inside the platform.

Which protocol does nginx ingress controller handle?

NGINX Ingress resources support additional protocols (TCP, UDP, and TLS Passthrough) – You can now deliver complex, non-HTTP-based services from Kubernetes using custom resources, in a simple and intuitive manner.

Does ingress controller need load balancer?

What is the Ingress? The Ingress is a Kubernetes resource that lets you configure an HTTP load balancer for applications running on Kubernetes, represented by one or more Services. Such a load balancer is necessary to deliver those applications to clients outside of the Kubernetes cluster.


2 Answers

Use following annotations in your ingress to set response header

    nginx.ingress.kubernetes.io/configuration-snippet: |
      more_set_headers "X-Frame-Options: Deny";
      more_set_headers "X-Xss-Protection: 1; mode=block";
      more_set_headers "X-Content-Type-Options: nosniff";
like image 72
hoque Avatar answered Oct 01 '22 16:10

hoque


The you are not able to find those headers as the traffic is flowing from a nginx ingress controller which acts as a proxy. To add some custom headers you can use the following given steps.

  1. create a file and name it as custom-headers.yml and add the following data.

    apiVersion: v1
    data:
      X-Frame-Options: "Deny"
      X-Xss-Protection: "1; mode=block"
      X-Content-Type-Options: "nosniff"
      kind: ConfigMap
    metadata:
    name: custom-headers
    namespace: ingress-nginx
    

This file will create a ConfigMap in the ingress-nginx namespace. Apply this ConfigMap: kubectl apply -f custom-headers.yml

Now we need to make our nginx ingress controller to use this new ConfigMap. For that we need to add our config map with the global configs that were being used until now. For that create a file configmap.yml and add the following data. apiVersion: v1 data: proxy-set-headers: "ingress-nginx/custom-headers" kind: ConfigMap metadata: name: nginx-configuration namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx

Apply this configuration by : kubectl apply -f configmap.yml

Check your configurations by using : kubectl exec <nginx-controller pod name> -n ingress-nginx cat /etc/nginx/nginx.conf

like image 21
ANKIT SURKAR Avatar answered Oct 01 '22 17:10

ANKIT SURKAR