How to configure Cors with a reverse proxy caddyfile

785 Views Asked by At

I've been trying to configure a simple load balancer with caddy server using reverse_proxy directive where i'm hosting my backend application but my frontend application is always blocked by cors policy. This is my Caddy file

(cors) {
        @origin header Origin "*"
        header @origin Access-Control-Allow-Origin "*"
        header @origin Access-Control-Allow-Methods "OPTIONS,HEAD,GET,POST,PUT,PATCH,DELETE"
        header @origin Access-Control-Allow-Headers "Content-Type,Authorization,Accept"
}

myDomain {
        import cors
        reverse_proxy * {
                to 127.0.0.1:8000
                to 127.0.0.1:8001
                to 127.0.0.1:8002
                to 127.0.0.1:8003
                to 127.0.0.1:8004
                to 127.0.0.1:8005
                lb_policy round_robin
                lb_try_duration 1s
                lb_try_interval 250ms
                
        }
}

I was looking inside the documentation provided by caddy add I found a simple example on how to use it in their official docs.basic example.But it did not work.I also looked for answers provided by their community . Here are some links https://caddyserver.com/docs/caddyfile/directives/import#examples https://caddy.community/t/cors-allow-origin-with-reverse-proxy/19355 But non of them worked because the syntax given is for old versions that are no longer supported

0

There are 0 best solutions below