如何调试从 HTTP 到 HTTPS 的 Kubernetes nginx Ingress 重定向

Opt*_*tio 6 https nginx kubernetes kubernetes-ingress nginx-ingress

快速问题:我如何调试入口和 Nginx 以了解 HTTP->HTTPS 重定向发生的确切位置?

更多细节:

我们有什么:我们有 war 文件 + Tomcat,用 Docker 构建它。在 AWS 中使用 Kubernetes 运行它。

我们需要的是:应用程序应该可以通过 HTTP 和 HTTPS 访问。HTTP不应重定向到 HTTPS。

问题:HTTP 总是重定向到 HTTPS。

我们的尝试:我们有 Ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ${some name
  namespace: ${some namespace}
  labels:
    app: ${some app}
    env: ${some env}
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false" #we added this for turn off https redirection
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false" #we added this for turn off https redirection
    nginx.ingress.kubernetes.io/affinity: "cookie" # We use it for sticky sessions
    nginx.ingress.kubernetes.io/affinity-mode: "persistent"
    nginx.ingress.kubernetes.io/session-cookie-name: "some cookie name"
    nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/whitelist-source-range: ${whitelist of ip adresses}

spec:
  tls:
    - hosts:
        - ${some host}
        - ${some another host}
      secretName: my-ingress-ssl
  rules:
    - host: ${some host}
      http:
        paths:
          - path: /
            backend:
              serviceName: ${some another service name}
              servicePort: 8080
    - host: ${some another host}
      http:
        paths:
          - path: /
            backend:
              serviceName: ${some another service name}
              servicePort: 8080
Run Code Online (Sandbox Code Playgroud)

和配置图

kind: ConfigMap
apiVersion: v1
metadata:
  labels:
    app: ${some app}
    env: ${some env}
  namespace: ${some namespace}
  name: nginx-config
data:
  hsts: "false" #we added this for turn off https redirection
  hsts-max-age: "0" #we added this for turn off https redirection
  ssl-redirect: "false" #we added this for turn off https redirection
  hsts-include-subdomains: "false" #we added this for turn off https redirection
Run Code Online (Sandbox Code Playgroud)

在 Tomcat server.xml 中,我们有:

<Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" />
Run Code Online (Sandbox Code Playgroud)

...

<!-- Define an AJP 1.3 Connector on port 8009 -->
        <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Run Code Online (Sandbox Code Playgroud)

...

和我们评论的这个连接器(它现在不应该工作):

<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
           maxThreads="150" SSLEnabled="true" >
    <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    <SSLHostConfig>
        <Certificate certificateKeyFile="conf/key.pem"
                     certificateFile="conf/cert.pem"
                     certificateChainFile="conf/chain.pem"
                     type="RSA" />
    </SSLHostConfig>
</Connector>
-->
Run Code Online (Sandbox Code Playgroud)

我尝试了所有可能的带有入口注释的变体,但没有成功。

我想知道的是:如何使用 Nginx调试入口以了解 HTTP->HTTPS 重定向发生的确切位置?

UPD

事实证明,安装在服务器上的不是 Nginx 控制器,而是 Traefik。由于安全限制,我无法看到带有控制器的吊舱。所以没有 Nginx 注释起作用。尽管如此,下面对我的问题的回答将帮助有类似问题的人。

Ric*_*ods 7

在您描述的设置中,至少有三个进程涉及处理传入的 http 请求

互联网 -> AWS ELB -> Nginx 入口控制器 -> Tomcat

任何进程都可以发出 301 重定向来指示 http 流量使用 https 重试。tomcat 进程中的重定向可以由 tomcat 配置或应用程序 tomcat 主机指定。

我会尝试通过尝试绕过更高的进程来隔离正在执行重定向的进程:

  1. 绕过负载均衡器。通过 SSH 连接到 K8s 节点之一,并在 URL 中卷曲入口服务的节点端口(请查看 nginx-controller 服务描述中的内部端点以获取正确的端口)。如果您收到重定向,您就知道问题不在于负载均衡器。
  2. 通过“docker exec”绕过入口控制器进入应用程序容器之一,然后 curl localhost:8080。如果您收到重定向,您就知道问题不在于入口控制器,而必须在 tomcat 配置或应用程序中。

如果由于 ssh 限制、curl 可用性等原因,这一切都不可能......另一种方法是为所涉及的每个进程打开请求日志记录 - 尽管这不适用于生产工作负载,因为它会导致机密信息被输出到不适当的上下文,例如 web 层日志文件或 cloudwatch 日志。