How to add add access control policy in json config

1. The problem I’m having:

cors error while calling api
Getting CORS error, web pages are at www.ab.com (CDN) and api on a.ab.com (caddy) ?

2. Error messages and/or full log output:

how to fix Referrer Policy: no-referrer-when-downgrade on caddy server with json config, getting CORS error.

3. Caddy version:

v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=

4. How I installed and ran Caddy:

In a docker swarm as a docker service, works/ starts fine.

a. System environment:

docker swarm

b. Command:

docker service create // config

c. Service/unit/compose file:

NA

d. My complete Caddy config:

{
    "admin": {
        "disabled": false
    },
    "logging": {
        "logs": {
            "": {
                "level": "DEBUG"
            }
        }
    },
    "apps": {
        "http": {
            "http_port": 80,
            "servers": {
                "example": {
                    "listen": [
                        ":443"
                    ],
                    "routes": [
                        {
                            "match": [
                                {
                                    "host": [
                                        "ab.com"
                                    ]
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "subroute",

                                    "routes": [
                                        {
                                            "handle": [
                                                {
                                                    "handler": "static_response",
                                                    "headers": {
                                                        "Location": [
                                                            "https://www.ab.com{http.request.uri}"
                                                        ]
                                                    },
                                                    "status_code": 302
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "terminal": true
                        },


                        {
                            "match": [
                                {
                                    "path": [
                                        "/chacode_mo_co_wss_4_s"
                                    ],
                                    "header": {
                                        "Access-Control-Allow-Origin":["*"],
                                          "Access-Control-Allow-Methods":["*"],
                                          "Access-Control-Allow-Headers":["*"],

                                        "Connection": [
                                            "*Upgrade*"
                                        ],
                                        "Upgrade": [
                                            "websocket"
                                        ]
                                    }
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "reverse_proxy",
                                    "transport":{
                                        "protocol": "http"
                                    },
                                    "upstreams": [
                                        {
                                            "dial": "aman-sock-4-s:80"
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "match": [
                                {
                                    "path": [
                                        "/tracker",
                                        "/mo_co_tracker_4_s"
                                    ],

                                    "header": {
                                        "Access-Control-Allow-Origin":["*"],
                                          "Access-Control-Allow-Methods":["*"],
                                          "Access-Control-Allow-Headers":["*"]
                                    }
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "reverse_proxy",
                                    "transport":{
                                        "protocol": "http"
                                    },
                                    "upstreams": [
                                        {
                                            "dial": "aman-tracker-4-s:80"
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "match": [
                                {
                                    "path": [
                                        "/mo_jo",
                                        "/mo_co",
                                        "/mo_co_c_2_s"
                                    ]
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "reverse_proxy",

                                    "header": {
                                        "Access-Control-Allow-Origin":["*"],
                                          "Access-Control-Allow-Methods":["*"],
                                          "Access-Control-Allow-Headers":["*"]
                                    },
                                    "transport": {
                                        "protocol": "http"
                                    },
                                    "upstreams": [
                                        {
                                            "dial": "aman-c-2-s:80"
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "match": [
                                {
                                    "path": [
                                        "/mo_co_filewrite_2_s",
                                        "/mo_co_filewrite_2_s/loadFile",
                                        "/gt1"
                                    ]
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "reverse_proxy",
                                    "transport": {
                                        "protocol": "http"
                                    },
                                    "upstreams": [
                                        {
                                            "dial": "aman-filewrite-2-s:80"
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "handle": [
                                {
                                    "handler": "reverse_proxy",
                                    "transport": {
                                        "protocol": "http"
                                    },
                                    "upstreams": [
                                        {
                                            "dial": "aman-a-4-s:80"
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "handle": [
                                {
                                    "handler": "static_response",
                                    "body": "Hello, world 23!"
                                }
                            ]
                        }
                    ],
                    "tls_connection_policies": [
                        {}
                    ],
                    "automatic_https": {
                        "disable_redirects": false
                    },
                    "logs": {}
                }
            }
        } 
    }
}

5. Links to relevant resources:

trying to refer to : JSON Config Structure - Caddy Documentation

Getting CORS error, web pages are at www.ab.com (CDN) and api on a.ab.com (caddy) ?
Request Method: OPTIONS
Status Code: 308
Note: i have a cloudflare as the DNS provider for CDN.
CAn i pls get some more examples around json config its really hard to start.

Please upgrade to the latest version, 2.7.4

I don’t understand the question. CORS problems are not server-side issues, they’re browser issues.

You can use the header directive to set response headers.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.