I have ASP.NET WebApi project, which return security headers on each request:
When I run this app in Kubernetes cluster with Ingress NGINX, my headers are missed
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
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.
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.
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.
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";
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With