Score:1

Multiple cors sites on Traefik

mq flag

I'm using Traefik on my Ubuntu server. Cors works fine for one website but when I try to use it for two they seem to conflict. What is the correct syntax?

This is what I have in my docker-compose file:

Service 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"

Service 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"
Score:1
by flag

You have the same CORS middleware, lets separate them in your docker-compose.yml file.

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"
Joe Gatt avatar
mq flag
That worked, thank you so much! I was under the wrong impression that I have to explicitly reference a middleware called cors.
Saxtheowl avatar
by flag
You welcome :))
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.