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

时间:2019-01-17 19:51:55

标签: azure kubernetes .net-core azure-aks nginx-ingress

我们正在按照以下指南在Azure上设置AKS群集:https://docs.microsoft.com/en-us/azure/aks/ingress-own-tls

我们在入口控制器后面运行5 .Net Core API,一切正常,请求被很好地路由。 但是,在我们的SPA Frontend中,我们正在向API发送自定义的HTTP标头,该标头似乎从未进入API,当我们检查AKS中的日志记录时,我们看到所需的http标头为空。 在开发中,一切正常,我们还看到在AKS的测试环境中填充了HTTP标头,因此我猜测Ingress阻止了这些自定义标头。

是否需要进行任何配置才能使入口通过自定义的HTTP标头?

编辑:

{
  "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": [
        {}
      ]
    }
  }
}

3 个答案:

答案 0 :(得分:3)

如果我希望入口控制器将自定义标头传递给后端服务,则可以在入口规则中使用此批注

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

答案 1 :(得分:2)

答案 2 :(得分:1)

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

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

nginx通过$http_前缀使所有自定义http标头作为嵌入式变量可用,请参见http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_