Trying to enable CORS

I have a nginx container behind traefik.
I enabled cors requests in nginx.
I swear this worked in a version of traefik2 around Jan 2020.
I haven't been able to make it work. I've read various posts about middleware but I'm not sure how to configure this correctly.

this is my traefik conf, using traefik 2.2.1

version: "2"
volumes:
  traefik-certs:
services:
  traefik:
    container_name: traefik
    image: traefik:latest
    command:
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --providers.docker
      - --certificatesresolvers.le.acme.email=${EMAIL}
      - --certificatesresolvers.le.acme.storage=/certs/acme.json
      - --certificatesresolvers.le.acme.tlschallenge=true
      - --api
    labels:
      - "traefik.http.routers.traefik.rule=Host(`${DOMAIN}`)"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.tls.certresolver=le"
      - "traefik.http.routers.traefik.entrypoints=websecure"
      - "traefik.http.routers.traefik.middlewares=authtraefik"
      - "traefik.http.routers.http-catchall.rule=hostregexp(`{host:.+}`)"
      - "traefik.http.routers.http-catchall.entrypoints=web"
      - "traefik.http.routers.http-catchall.middlewares=redirect-to-https"
      - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - traefik-certs:/certs
    networks:
      - traefik
    mem_limit: 96m
networks:
  traefik:

my nginx conf

server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

What does the router for nginx look like?

    labels:
      - "traefik.http.routers.nginx-server.rule=Host(`server.net`, `www.server.net`)"
      - "traefik.http.routers.nginx-server.tls.certresolver=le"
      - "traefik.http.routers.nginx-server.entrypoints=websecure"
      - "traefik.http.services.nginx-server.loadbalancer.server.port=80"
      - "traefik.docker.network=traefik_traefik"

So it looks like you have nginx taking care of CORS entirely.

Are you asking how to get CORS from the traefik middleware or are you having an issue with the CORS headers from nginx via traefik?

So it looks like you have nginx taking care of CORS entirely.

When I ran latest traefik2 in Jan 2020 , I configured nginx to serve CORS headers. They were passed successfully from nginx through traefik to clients.

Something is different now in traefik2 . the CORS headers are not received by clients.

I am happy to just use traefik2 to send CORS headers if I have to.

I don't know how.

1 Like

An app that I am proxying send CORS headers. They came though. Using V2.2

Have you exposed the nginx container and tested directly?

I tested with and without traefik. see the difference?

$ cat test-cors-with-nginx-and-traefik.sh
export MY_URL="https://myurl.com"
curl -I -X OPTIONS \\n  -H "Origin: ${MY_URL}" \\n  -H 'Access-Control-Request-Method: GET' \\n  "${MY_URL}/r.mp4" 2>&1 | grep -i 'Access-Control-Allow-Origin'
    ​
$ test-cors-with-only-nginx.sh
export MY_URL="http://myurl.com:55555"
    ​
curl -I -X OPTIONS \\n  -H "Origin: ${MY_URL}" \\n  -H 'Access-Control-Request-Method: GET' \\n  "${MY_URL}/r.mp4" 2>&1 | grep -i 'Access-Control-Allow-Origin'
    ​
$ ./test-cors-with-nginx-and-traefik.sh
access-control-allow-origin: *
$ ./test-cors-with-only-nginx.sh
Access-Control-Allow-Origin: *

Curious, there, but lower cased.

right.

the Thingie I'm doing would like it not all be lowercase.
I can't find anything in the Google about this.