Traefik Ingress路径替换求助:外部/api需重写为后端/ngsi-ld
解决Traefik Ingress路径重写:将/api替换为/ngsi-ld转发到Scorpio后端
你之前的问题核心是误用了Nginx Ingress Controller的注解,但实际使用的是Traefik,所以那些配置不会生效。以下是针对Traefik的正确配置方案:
方案:使用Traefik ReplacePathRegex中间件
Traefik通过CRD类型的Middleware实现路径重写,需要分两步配置:
1. 创建ReplacePathRegex中间件
这个中间件会将外部请求的/api/xxx路径替换为后端需要的/ngsi-ld/xxx:
apiVersion: traefik.containo.us/v1alpha1 kind: Middleware metadata: name: replace-api-with-ngsi-ld namespace: default # 替换为你的资源所在命名空间 spec: replacePathRegex: regex: ^/api/(.*) replacement: /ngsi-ld/$1
2. 配置Ingress并引用中间件
在Ingress中指定路径为/api,并关联上面创建的中间件:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: scorpio-ingress namespace: default # 与中间件同命名空间 annotations: # 中间件引用格式:<命名空间>-<中间件名称>@kubernetescrd traefik.ingress.kubernetes.io/router.middlewares: default-replace-api-with-ngsi-ld@kubernetescrd spec: rules: - host: myservice.com http: paths: - path: /api pathType: Prefix backend: service: name: scorpio port: number: 9090
配置说明
- 当外部请求
http://myservice.com/api/v1/entities?type=data时,中间件会将路径重写为/ngsi-ld/v1/entities,然后转发到Scorpio后端的9090端口,完全匹配后端的路径需求。 - 确保中间件和Ingress在同一个命名空间,或者在引用时正确指定命名空间。
内容的提问来源于stack exchange,提问作者pasquy73
相关产品推荐
相关产品推荐

