Skip to content

Cross-Origin Resource Sharing (CORS)

A cross-origin request occurs when a webpage on one origin (for example, a.example.com) requests a resource from a different origin (for example, b.secondexample.com). Cross-Origin Resource Sharing (CORS) is a mechanism that uses HTTP headers to let the server at b.secondexample.com indicate whether a.example.com is allowed to access its resources. Browsers enforce these headers and block access to responses that are not permitted.

Cloudflare supports CORS by:

  • Identifying cached assets based on the Host Header, Origin Header, URL path, and query. This allows different resources to use the same Host header but different Origin headers.
  • Passing Access-Control-Allow-Origin headers from the origin server to the browser.

The Access-Control-Allow-Origin header lets a server specify rules for sharing its resources with external origins. A server may respond with different Access-Control-Allow-Origin values depending on the Origin header in the request. These headers are often present on cacheable content.

Add or change CORS headers at the origin server

If you add or change CORS configuration at your origin web server, purging the Cloudflare cache by URL does not update the CORS headers. Force Cloudflare to retrieve the new CORS headers via one of the following options:

  • Change the filename or URL to bypass cache to instruct Cloudflare to retrieve the latest CORS headers.
  • Use the single-file purge API to specify the appropriate CORS headers along with the purge request.
  • Update the resource’s last-modified time at your origin web server. Then, complete a full purge to retrieve the latest version of your assets including updated CORS headers.

Add or change CORS headers on Cloudflare

You can use one of following methods to set CORS headers using Cloudflare products: