Traefik 上的多个 cors 站点

Traefik 上的多个 cors 站点

我在 Ubuntu 服务器上使用 Traefik。Cors 对一个网站来说运行良好,但当我尝试将它用于两个网站时,它们似乎发生冲突。正确的语法是什么?

这是我在 docker-compose 文件中的内容:

服务1:

  - "traefik.http.routers.service-one.middlewares=cors"
  - "traefik.http.middlewares.cors.headers.accesscontrolallowmethods=GET,OPTIONS,PUT"
  - "traefik.http.middlewares.cors.headers.accesscontrolalloworiginlist=https://site1.com,https://site2.com,https://site2.com"
  - "traefik.http.middlewares.cors.headers.accesscontrolmaxage=100"
  - "traefik.http.middlewares.cors.headers.addvaryheader=true"

服务2:

  - "traefik.http.routers.service-two.middlewares=cors"
  - "traefik.http.middlewares.cors.headers.accesscontrolallowmethods=GET,OPTIONS,PUT"
  - "traefik.http.middlewares.cors.headers.accesscontrolalloworiginlist=https://site1.com,https://site2.com,https://site2.com"
  - "traefik.http.middlewares.cors.headers.accesscontrolmaxage=100"
  - "traefik.http.middlewares.cors.headers.addvaryheader=true"

答案1

您有相同的 CORS 中间件,让我们在您的docker-compose.yml文件中将它们分开。

services:
  service1:
    labels:
      - "traefik.http.routers.service-one.middlewares=cors-service1"
      - "traefik.http.middlewares.cors-service1.headers.accesscontrolallowmethods=GET,OPTIONS,PUT"
      - "traefik.http.middlewares.cors-service1.headers.accesscontrolalloworiginlist=https://site1.com,https://site2.com"
      - "traefik.http.middlewares.cors-service1.headers.accesscontrolmaxage=100"
      - "traefik.http.middlewares.cors-service1.headers.addvaryheader=true"

  service2:
    labels:
      - "traefik.http.routers.service-two.middlewares=cors-service2"
      - "traefik.http.middlewares.cors-service2.headers.accesscontrolallowmethods=GET,OPTIONS,PUT"
      - "traefik.http.middlewares.cors-service2.headers.accesscontrolalloworiginlist=https://site1.com,https://site2.com"
      - "traefik.http.middlewares.cors-service2.headers.accesscontrolmaxage=100"
      - "traefik.http.middlewares.cors-service2.headers.addvaryheader=true"

相关内容