You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于请求头(User Agent)的Kubernetes Ingress路由配置示例求助

Kong Ingress 路由配置示例(无DB模式)

核心逻辑是通过User-Agent请求头区分流量来源:

  • 浏览器发起的请求(User-Agent不含"Electron"):重定向至Electron应用下载地址
  • Electron应用发起的请求(User-Agent含"Electron"):转发至前端服务

以下是完整的Ingress配置示例:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kong-traffic-routing
  annotations:
    # 指定由Kong Ingress Controller处理该Ingress
    kubernetes.io/ingress.class: kong
    # 根据前端服务需求调整是否剥离路径
    konghq.com/strip-path: "true"
spec:
  rules:
    - host: your-domain.com  # 替换为你的实际业务域名
      http:
        paths:
          # 规则1:转发Electron应用流量到前端服务
          - path: /
            pathType: Prefix
            backend:
              service:
                name: frontend-service  # 替换为你的前端服务名称
                port:
                  number: 80
            annotations:
              # 匹配User-Agent包含"Electron"的请求
              konghq.com/match-user-agent: "Electron"
          # 规则2:重定向浏览器流量到下载地址
          - path: /
            pathType: Prefix
            backend:
              # 可指向一个空ClusterIP服务,请求会被重定向插件拦截
              service:
                name: dummy-service
                port:
                  number: 80
            annotations:
              # 匹配User-Agent不包含"Electron"的请求
              konghq.com/match-user-agent: "!(Electron)"
              # 配置重定向插件
              konghq.com/plugins: |
                - name: redirect-to
                  config:
                    status_code: 302
                    url: "https://your-download-domain.com/electron-installer.exe"  # 替换为实际下载地址

配置细节说明

  1. Ingress Class:kubernetes.io/ingress.class: kong是无DB模式下的核心标识,确保Kong Controller处理该路由规则。
  2. 空服务创建:规则2中的dummy-service无需实际后端,仅作为路由载体,可通过以下命令快速创建:
    kubectl create service clusterip dummy-service --tcp=80:80
    
  3. 重定向规则:redirect-to插件的status_code可根据需求调整为301(永久重定向)或302(临时重定向)。
  4. UA匹配逻辑:!(Electron)表示排除所有带Electron标识的请求,确保浏览器流量被正确拦截。

验证方法

部署配置后,可通过curl命令测试流量走向:

  • 模拟浏览器请求:
    curl -v https://your-domain.com -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
    
    应返回302重定向到下载地址。
  • 模拟Electron请求:
    curl -v https://your-domain.com -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Electron/28.0.0"
    
    应返回前端服务的正常响应。

内容的提问来源于stack exchange,提问作者Abdullah javed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 22:09:15