What is CORS? The browser rule behind every cross-origin error
Simple requests, preflights and credentials, with curl tests.
Read the guideCDN.com.tr Help
Add Access-Control-Allow-Origin and the other CORS headers to a delivery rule's responses, let your origin answer preflight OPTIONS requests, and allow the methods your API uses in the WAF.
Cache, security, WAF, and purge
Add Access-Control-Allow-Origin and the other CORS headers to a delivery rule's responses, let your origin answer preflight OPTIONS requests, and allow the methods your API uses in the WAF.
https://app.example.com, or * for files anyone may load.OPTIONS on those paths with 200 or 204.What the browser sends decides where the work happens.
GET: no preflight, and Access-Control-Allow-Origin in the rule is all they need. While account-wide image optimization is on, .jpg, .jpeg and .png files take the response headers of the domain's oldest rule, not of the rule for their path.PUT, PATCH, DELETE: the browser sends a preflight first; your origin answers it, and the rule can add the CORS headers to that answer.*, plus Access-Control-Allow-Credentials true.Pick one source per header.
always.Vary: Origin.Example for an API under /api/ that one web app calls. Write one header per line; a value with spaces goes in double quotes.
/api/.Access-Control-Allow-Origin https://app.example.com, Access-Control-Allow-Methods "GET, POST, PUT, OPTIONS", Access-Control-Allow-Headers "Content-Type, Authorization" and Access-Control-Max-Age 600.Expected result: The rule shows a chip such as +4 response headers, and the page shows Live once the edge serves it.
Needed while the site-wide WAF is on and the API uses methods beyond GET, HEAD, POST and OPTIONS.
PUT, PATCH or DELETE as your API needs, keep OPTIONS ticked, and press Save.Expected result: The panel confirms the save, and the change propagates within 1-2 minutes.
curl does not enforce CORS, so it shows exactly what the edge answers.
Origin header.OPTIONS with Origin, Access-Control-Request-Method and Access-Control-Request-Headers.Expected result: The simple request carries one access-control-allow-origin. The preflight answers 200 or 204 with the allowed origin, methods and headers.
curl -s -o /dev/null -D - -H 'Origin: https://app.example.com' https://www.example.com/api/items | grep -iE '^(HTTP|access-control)'
curl -s -o /dev/null -D - -X OPTIONS -H 'Origin: https://app.example.com' -H 'Access-Control-Request-Method: PUT' -H 'Access-Control-Request-Headers: content-type' https://www.example.com/api/items | grep -iE '^(HTTP|access-control|x-mt-blocked-by)'
A web app on app.example.com loads fonts, JSON or API responses from www.example.com, and the browser blocks them with a CORS error.
Header-Name value, without a colon, for example Access-Control-Allow-Origin https://app.example.com. A value with spaces goes in double quotes.Access-Control-* header for these paths, tick the same name under Remove headers, so the browser receives a single value.PUT, PATCH or DELETE and the site-wide WAF is on, tick them under Allowed HTTP methods on the Security page.always is also added to your origin's 4xx and 5xx responses.OPTIONS requests go to your origin and are never cached. When your origin answers a preflight with 200 or 204, the rule's headers are added to that answer; a preflight answered with 401, 404 or 405 reaches the browser without them, unless the line ends with always. On a rule with Password protection or an IP allow list, the edge itself answers a preflight it does not let through, with 401 or 403.Access-Control-Allow-Origin reaches the browser: with two values, the browser rejects the response.Access-Control-Allow-Origin by the requesting site, it must also send Vary: Origin. The edge keeps those variants apart as long as Keep one cached copy whatever the Vary header says is off on the rule.Access-Control-Request-Method and Access-Control-Request-Headers unticked under Remove headers: the names ticked there are also removed from the requests sent to your origin, and your origin reads these two to answer a preflight.GET, HEAD, POST and OPTIONS are allowed by default. A request with a method that is not on the list gets a 403 with no CORS headers and with X-MT-Blocked-By: cdn-edge-security, which the browser reports as a CORS error..jpg, .jpeg and .png files get the response headers of the domain's oldest rule, not of the rule for their path: put the CORS header for those images on that rule, with a fixed Access-Control-Allow-Origin. When that oldest rule is the certificate-check rule, they get no rule headers.The edge passes OPTIONS requests to your origin and never caches them, so your origin must answer the preflight with 200 or 204. The rule's response headers are added to that answer: the CORS headers can come from the rule, and your origin only has to accept OPTIONS. The exception: on a rule with Password protection or an IP allow list, the edge itself answers a preflight it does not let through, with 401 or 403.
A rule adds the same value to every response: * or one origin such as https://app.example.com. To allow several origins, let your application send the matching origin together with Vary: Origin, and leave Access-Control-Allow-Origin out of the rule. Requests with cookies or other credentials need a named origin, not *.
Start with the status of the failing request in the browser's network panel. A 403 with X-MT-Blocked-By: cdn-edge-security comes from the edge: look it up in WAF logs, and if its method is missing, add it under Allowed HTTP methods. By default, any other 4xx or 5xx arrives without the rule's headers, so fix that response first. A line that ends with always, such as Access-Control-Allow-Origin https://app.example.com always, is also added to your origin's 4xx and 5xx responses. Then check that exactly one Access-Control-Allow-Origin arrives and that it matches the requesting site, scheme included.
While the site-wide WAF is on, open the Security page, tick the methods under Allowed HTTP methods and press Save; the change propagates within 1-2 minutes. Allowed request methods on the Security tab of Delivery Rules opens the same card. Keep OPTIONS ticked: browsers send it for preflights.