配置 Ingress 控制器以转发自定义 http headers

Configure Ingress controller to forward custom http headers

我们正在 Azure 上设置 AKS 集群,遵循 this guide

我们 运行 5 .Net Core API 在入口控制器后面,一切正常,请求被很好地路由。 但是,在我们的 SPA 前端中,我们正在向我们的 API 发送自定义 http header,这个 header 似乎永远不会到达 API,当我们检查 AKS 中的日志记录,我们看到所需的 http header 是空的。 在开发中,一切正常,我们还看到 AKS 中的测试环境中填充了 http header,所以我猜入口阻止了这些自定义 headers.

是否需要任何配置才能使入口通过自定义 http headers?

编辑:

{
  "kind": "Ingress",
  "apiVersion": "extensions/v1beta1",
  "metadata": {
    "name": "myappp-ingress",
    "namespace": "myapp",
    "selfLink": "/apis/extensions/v1beta1/namespaces/myapp/ingresses/myapp-ingress",
    "uid": "...",
    "resourceVersion": "6395683",
    "generation": 4,
    "creationTimestamp": "2018-11-23T13:07:47Z",
    "annotations": {
      "kubernetes.io/ingress.class": "nginx",
      "nginx.ingress.kubernetes.io/allow-headers": "My_Custom_Header", //this doesn't work
      "nginx.ingress.kubernetes.io/proxy-body-size": "8m",
      "nginx.ingress.kubernetes.io/rewrite-target": "/"
    }
  },
  "spec": {
    "tls": [
      {
        "hosts": [
          "myapp.com"
        ],
        "secretName": "..."
      }
    ],
    "rules": [
      {
        "host": "myapp.com",
        "http": {
          "paths": [
            {
              "path": "/api/tenantconfig",
              "backend": {
                "serviceName": "tenantconfig-api",
                "servicePort": 80
              }
            },
            {
              "path": "/api/identity",
              "backend": {
                "serviceName": "identity-api",
                "servicePort": 80
              }
            },
            {
              "path": "/api/media",
              "backend": {
                "serviceName": "media-api",
                "servicePort": 80
              }
            },
            {
              "path": "/api/myapp",
              "backend": {
                "serviceName": "myapp-api",
                "servicePort": 80
              }
            },
            {
              "path": "/app",
              "backend": {
                "serviceName": "client",
                "servicePort": 80
              }
            }
          ]
        }
      }
    ]
  },
  "status": {
    "loadBalancer": {
      "ingress": [
        {}
      ]
    }
  }
}

如果我希望我的入口控制器将自定义 header 传递到我的后端服务,我可以在我的入口规则中使用此注释

nginx.ingress.kubernetes.io/configuration-snippet: |
  more_set_headers "Request-Id: $req_id";

我最终使用了以下配置片段:

nginx.ingress.kubernetes.io/configuration-snippet: |
  proxy_set_header My-Custom-Header $http_my_custom_header;

nginx 通过 $http_ 前缀使所有自定义 http headers 可用作嵌入式变量,请参阅 this

默认入口 doesn’t pass through headers with underscores。 你可以设置

enable-underscores-in-headers: true

https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#enable-underscores-in-headers

还有 Ingress doesn’t pass through Authorization header