关于asp.net Web API:为什么Ingress NGINX删除了我的响应头

Why Ingress NGINX removes my response headers

我有ASP.NET WebApi项目,该项目在每个请求上返回安全标头:

enter


在入口中使用以下注释来设置响应标头

1
2
3
4
    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";


您无法找到这些标头,因为流量是从充当代理的nginx入口控制器流出的。要添加一些自定义标头,可以使用以下给定步骤。

  • 创建一个文件并将其命名为custom-headers.yml并添加以下数据。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    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
  • 此文件将在ingress-nginx命名空间中创建ConfigMap。应用此ConfigMap:
    kubectl apply -f custom-headers.yml

    现在,我们需要使我们的nginx入口控制器使用此新的ConfigMap。为此,我们需要添加带有到目前为止使用的全局配置的配置映射。为此,创建一个文件configmap.yml并添加以下数据。
    apiVersion:v1
    数据:
    代理设置标题:" ingress-nginx /自定义标题"
    种类:ConfigMap
    元数据:
    名称:nginx配置
    命名空间:ingress-nginx
    标签:
    app.kubernetes.io/名称:ingress-nginx
    app.kubernetes.io/部分:ingress-nginx

    通过以下方式应用此配置:
    kubectl apply -f configmap.yml

    使用以下命令检查您的配置:kubectl exec <nginx-controller pod name> -n ingress-nginx cat /etc/nginx/nginx.conf


    我自己还没有尝试过,但是我认为您需要启用此设置。

    https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#allow-backend-server-header

    1
    2
    data:
        allow-backend-server:"true"

    您可以尝试通过注释启用此设置。