Edit

Share via


Using Azure CDN with CORS

Important

Azure CDN Standard from Microsoft (classic) will be retired on September 30, 2027. To avoid any service disruption, it's important that you migrate your Azure CDN Standard from Microsoft (classic) profiles to Azure Front Door Standard or Premium tier by September 30, 2027. For more information, see Azure CDN Standard from Microsoft (classic) retirement.

Azure CDN from Edgio was retired on January 15, 2025. For more information, see Azure CDN from Edgio retirement FAQ.

What is CORS?

CORS (cross-origin resource sharing) is an HTTP feature that enables a web application running under one domain to access resources in another domain. In order to reduce the possibility of cross-site scripting attacks, all modern web browsers implement a security restriction known as same-origin policy. This restriction prevents a web page from calling APIs in a different domain. CORS provides a secure way to allow one origin (the origin domain) to call APIs in another origin.

How it works

There are two types of CORS requests, simple requests and complex requests.

For simple requests:

  1. The browser sends the CORS request with an extra Origin HTTP request header. The value of the request header is the origin that served the parent page, which is defined as the combination of protocol, domain, and port. When a page from HTTPS://www.contoso.com attempts to access a user's data in the fabrikam.com origin, the following request header would be sent to fabrikam.com:

    Origin: https://www.contoso.com

  2. The server might respond with any of the following headers:

    • An Access-Control-Allow-Origin header in its response indicating which origin site is allowed. For example:

      Access-Control-Allow-Origin: https://www.contoso.com

    • An HTTP error code such as 403 if the server doesn't allow the cross-origin request after checking the Origin header

    • An Access-Control-Allow-Origin header with a wildcard that allows all origins:

      Access-Control-Allow-Origin: *

For complex requests:

A complex request is a CORS request where the browser is required to send a preflight request (that is, a preliminary probe) before sending the actual CORS request. The preflight request asks the server permission if the original CORS request can proceed and is an OPTIONS request to the same URL.

Tip

For more details on CORS flows and common pitfalls, view the Guide to CORS for REST APIs.

Wildcard or single origin scenarios

CORS on Azure CDN works automatically without extra configurations when the Access-Control-Allow-Origin header is set to wildcard (*) or a single origin. CDN cache the first response and subsequent requests use the same header.

If requests have already been made to the CDN prior to CORS being set on your origin, you need to purge content on your endpoint content to reload the content with the Access-Control-Allow-Origin header.