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

GKE Ingress路径配置异常:请求未按预期剥离路径前缀

解决GKE GCE Ingress路径转发前缀保留问题

问题原因

GCE类型的Ingress默认会完整保留请求路径转发至后端服务。比如配置path: /customer时,访问/customer的请求会被转发到后端服务的/customer路径,而非预期的根路径/。即使修改pathType为Prefix,也不会自动去掉路径前缀。

解决方案

需要通过BackendConfig资源配置路径重写规则,配合Ingress的路径匹配实现前缀剥离。

1. 创建BackendConfig资源

分别为两个服务创建路径重写的BackendConfig:

客户服务BackendConfig

apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: customer-backend-config
spec:
  urlRewrite:
    pathPrefixRewrite: "/"

供应商服务BackendConfig

apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: supplier-backend-config
spec:
  urlRewrite:
    pathPrefixRewrite: "/"

2. 修改Ingress配置

更新Ingress的路径规则为前缀匹配(/{service}/*),并通过annotation关联对应的BackendConfig:

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: saas-ingress
  annotations:
    kubernetes.io/ingress.class: "gce"
    kubernetes.io/ingress.allow-http: "true"
    # 关联BackendConfig:指定每个服务对应的路径重写配置
    cloud.google.com/backend-config: '{"services": [{"name": "customerapp-service", "backendConfig": "customer-backend-config"}, {"name": "supplierapp-service", "backendConfig": "supplier-backend-config"}]}'
spec:
  rules:
  - http:
      paths:
      - path: /customer/*
        pathType: Prefix
        backend:
          service:
            name: customerapp-service
            port:
              number: 9001
      - path: /supplier/*
        pathType: Prefix
        backend:
          service:
            name: supplierapp-service
            port:
              number: 9000

3. 应用配置

执行以下命令部署资源:

kubectl apply -f customer-backend-config.yaml
kubectl apply -f supplier-backend-config.yaml
kubectl apply -f saas-ingress.yaml

关键说明

  • 该方案要求GKE集群版本≥1.18(urlRewrite特性从该版本开始支持)
  • path: /customer/*配合pathType: Prefix会匹配所有以/customer开头的请求(包括/customer、/customer/detail等)
  • pathPrefixRewrite: "/"会将请求路径中的/customer前缀剥离,转发到后端服务的根路径

内容的提问来源于stack exchange,提问作者dimas firdaus

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 17:40:28