traefik配置跨域策略

警告
本文最后更新于 2022-04-26 11:50,文中内容可能已过时。

1.这里用到traefik的middleware,用于添加响应头

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: test-cors
  namespace: default
spec:
  headers:
    customResponseHeaders: 
      Access-Control-Allow-Origin: "http://soulchild.cn:8080"
      Access-Control-Allow-Methods: "*"
      Access-Control-Allow-Headers: "*"
    # 另一种写法
    #accessControlAllowMethods:
    #  - "*"
    #accessControlAllowOriginList:
    #  - "http://soulchild.cn:8080"
    #accessControlAllowHeaders:
    #  - "*"
    #accessControlMaxAge: 100
    #addVaryHeader: true

2.和路由关联

这里说两种,一种是ingress资源,另一种是ingressroute

在ingress中添加annotations就可以关联了

1
traefik.ingress.kubernetes.io/router.middlewares: default-test-cors@kubernetescrd

如果使用的是ingressroute

1
2
3
4
5
6
7
8
9
spec:
  entryPoints:
  - web
  routes:
  - kind: Rule
    match: PathPrefix(`/api/xxx`)
    middlewares:
    - name: test-cors
      namespace: default
请我喝杯水
SoulChild 微信号 微信号
SoulChild 微信打赏 微信打赏
0%