首页 > 解决方案 > GKE Nginx 入口 - 设置主机导致 400 不需要 SSL 证书

问题描述

我在 GKE 1.16.13-gke.401 中使用 nginx 入口。

我的配置

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: api-ingress
  namespace: development
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    kubernetes.io/ingress.class: "nginx"
    # Enable client certificate authentication
    nginx.ingress.kubernetes.io/auth-tls-verify-client: "on"
    # Create the secret containing the trusted ca certificates
    nginx.ingress.kubernetes.io/auth-tls-secret: "development/client-cert-api-ingress"
    # Specify the verification depth in the client certificates chain
    nginx.ingress.kubernetes.io/auth-tls-verify-depth: "1"
    # Automatically redirect http to https
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    # Use regex in paths
    nginx.ingress.kubernetes.io/use-regex: "true"
    # For notifications we add the proxy headers
    nginx.ingress.kubernetes.io/configuration-snippet: |  
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
spec:
  tls:
    - hosts:
      - example.com
      secretName: api-tls-certificate
  rules:
  - host: example.com
    http:
      paths:
      - path: /(.*)
        backend:
          serviceName: v2-api-frontend
          servicePort: 443

Nginx 控制器配置:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingressnginx/controller-v0.40.2/deploy/static/provider/cloud/deploy.yaml

问题

指定时调用curl -k --cert my-cert.pem --key my-cert.key https://MY_IP/ --header "Host: example.com"不起作用rules.host

如果我指定rules.host服务器响应

400 Bad Request 未发送所需的 SSL 证书

如果我没有指定rules.hostcurl 工作正常。

我怎样才能解决这个问题?

编辑1:

我签发的自签名客户端证书似乎导致了问题。

使用以下脚本创建:

#!/bin/bash
# USE: ./build-client-cert.sh <CERTNAME> <EXPR_DAYS>

# How to generate client certificate
# based on https://www.makethenmakeinstall.com/2014/05/ssl-client-authentication-step-by-step/

openssl genrsa -out client.key 4096
openssl req -new -key client.key -out client.req
openssl x509 -req -in client.req -CA rootCA.crt -CAkey rootCA.key -set_serial 101 -extensions client -days $2 -outform PEM -out client.cer
openssl pkcs12 -export -inkey client.key -in client.cer -out $1.p12
rm client.key client.cer client.req

使用 cloudflare 时,服务器会正​​确响应,只有在通过 curl 使用我的证书时才会发生这种情况。

编辑 2

使用以下脚本创建 CA:

#!/bin/bash

# https://gist.github.com/fntlnz/cf14feb5a46b2eda428e000157447309

openssl genrsa -des3 -out rootCA.key 4096

openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.crt

标签: nginxgoogle-kubernetes-enginekubernetes-ingressnginx-ingress

解决方案


推荐阅读