After you add your website to Alibaba Cloud CDN for content delivery, you can configure custom HTTP response headers to enable cross-origin access.
What is CORS?
Cross-origin resource sharing (CORS) is a standard cross-origin solution that is provided by HTML5 to allow web pages to access and load resources from different origin servers. This ensures data transmission security. For more information, see Cross-Origin Resource Sharing (CORS).
Benefits
For security purposes, browsers use the same-origin policy. This restricts web pages from initiating requests to access and load resources from different domains or subdomains, or over different protocols or ports. For example, example.com
cannot access resources on example.org
. When you configure CORS, you can specify response headers on the CDN server. If a request to access CDN resources carries headers that comply with the allowed rules, cross-origin resources can be loaded and accessed.
How CORS works
CORS disabled
A client sends a request for
http://example.com/index.html
toexample.com
.example.com
returns theindex.html
file to the client. The file URL contains the URL to a resource from another origin server:http://example.org/image_01.png
.The client sends a request for
http://example.org/image_01.png
toexample.org
.example.org
returns a cross-origin header, such asAccess-Control-Allow-Origin: *
.The client receives the response and the browser checks the header value. The header value is an asterisk (*), which indicates that cross-origin requests from all origins are allowed. Then, the browser displays the response.
CORS enabled
A client sends a request for
http://example.com/index.html
to CDN.CDN returns the
index.html
file to the client. The file URL contains the URL to a resource from another origin server:http://example.org/image_01.png
.The client sends a request for
http://example.org/image_01.png
to CDN. The request includes theOrigin:http://example.com
header.CDN receives the request and then compares the value of the
Origin
header in the request with the configured value ofAccess-Control-Allow-Origin
. You can specify one or more values, or a wildcard domain name forAccess-Control-Allow-Origin
.Access-Control-Allow-Origin
is returned to the client only if the preceding header values match.CDN returns a cross-origin header, such as
Access-Control-Allow-Origin:http://example.com
, to the client.The client receives the response and the browser checks the header value. The header value is
http://example.com
, which indicates that cross-origin requests fromhttp://example.com
are allowed. Then, the browser displays the response.
Usage notes
If the origin server is an Object Storage Service (OSS) bucket and CORS is configured in both the OSS and CDN consoles, the CORS settings in the CDN console are used. For more information about CORS settings in the OSS console, see CORS.
If the origin server is an on-premises server or an Elastic Compute Service (ECS) instance, we recommend that you separate dynamic content from static content. This way, the delivery of static content is accelerated by CDN. The CORS settings in the CDN console take effect only for static resources.
Enable CORS
Log on to the Alibaba Cloud CDN console.
In the left-side navigation pane, click Domain Names.
On the Domain Names page, find the domain name that you want to manage and click Manage in the Actions column.
In the left-side navigation tree of the domain name, click Cache.
Click the POP HTTP Response Header tab.
Click Customize to configure HTTP response headers.
Configure the parameters to specify the allowed origins for cross-origin requests, and then click OK.
The following table describes the parameters.
Parameter
Description
Example
Operation
You must set this parameter to Add to use the CORS feature.
Add
Response Header
You must set this parameter to
Access-Control-Allow-Origin
to use the CORS feature.Access-Control-Allow-Origin
Header Value
CORS disabled: You can only configure a wildcard or a single specified origin.
Wildcard (
*
): A wildcard is configured, indicating that resources can be accessed from all origins.Single specified origin: A single origin (domain name) is specified, indicating that resources can be accessed only from the specified origin.
CORS enabled: You can configure a wildcard, a single specified origin, multiple specified origins, and wildcard domain names.
Wildcard (
*
): A wildcard is configured, indicating that resources can be accessed from all origins.Single specified origin: A single origin (domain name) is specified, indicating that resources can be accessed only from the specified origin.
Multiple specified origins: Multiple origins (domain names) are specified and separated with commas (
,
), indicating that resources can be accessed from origins within the specified range.Wildcard domain names: A wildcard domain name is configured, indicating that resources can be accessed from all origins that match the specified wildcard domain name.
*
http://www.aliyun.com
https://aliyun.com,http://www.aliyun.com
http://*.aliyun.com
Allow Duplicates
Yes: Duplicate headers are allowed. The header that is returned from the origin server and the header that is added to the response are returned to the client.
No: Duplicate headers are not allowed. The header that is returned from the origin server is overwritten by the header that is added to the response.
ImportantThe Allow Duplicates and CORS settings are mutually exclusive. If you set Allow Duplicates to Yes, the setting of CORS becomes invalid.
No
CORS
You can configure the CORS parameter only if you set Operation to Add and Response Header to
Access-Control-Allow-Origin
.Valid values of CORS: Disable and Enable. Default value: Disable.
Disable: CDN POPs do not check the
Origin
header in user requests. In this case, POPs return the configured value ofAccess-Control-Allow-Origin
.Enable: CDN POPs check the
Origin
header in user requests and specify a value for theAccess-Control-Allow-Origin
header based on the following rules:Wildcard pattern match: If you set the
Access-Control-Allow-Origin
header to an asterisk (*),Access-Control-Allow-Origin:*
is returned, regardless of whether user requests contain theOrigin
header or the value that is specified for theOrigin
header.Exact match: You can specify one or more values for the
Access-Control-Allow-Origin
header. Separate multiple values with commas (,
):If the value of the
Origin
header in a user request matches a value of theAccess-Control-Allow-Origin
header, the matched value of theAccess-Control-Allow-Origin
header is returned.If the value of the
Origin
header in a user request does not match a value of theAccess-Control-Allow-Origin
header, theAccess-Control-Allow-Origin
header is not returned.
Wildcard domain name match: If you set the
Access-Control-Allow-Origin
header to a wildcard domain name, the value of theOrigin
header is matched against the wildcard domain name.
If you set the CORS parameter to Enable and the value that you want to specify for the Header Value parameter contains a hyphen (
-
), you need to escape the hyphen (-
) to%-
. Examples:Original response header value:
http://doc.aliyun-example.com
.Escaped response header value:
http://doc.aliyun%-example.com
.
Enable
Configure the parameters to specify the allowed origins for cross-origin requests, and then click OK.
The following table describes the parameters.
Parameter
Description
Example
Operation
You must set this parameter to Add.
Add
Response Header
You must set this parameter to
Access-Control-Allow-Methods
.Access-Control-Allow-Methods
Header Value
Valid values:
GET
,POST
, andPUT
. If you addGET
,POST
, andPUT
at the same time, separate them with commas (,
).GET
Allow Duplicates
Yes: Duplicate headers are allowed. The header that is returned from the origin server and the header that is added to the response are returned to the client.
No: Duplicate headers are not allowed. The header that is returned from the origin server is overwritten by the header that is added to the response.
No
Examples
Example 1
Assume that you set the Access-Control-Allow-Origin
header to one or more values that are separated by commas (,
):
If the value of the
Origin
header in a user request matches a value of theAccess-Control-Allow-Origin
header, the matched value of theAccess-Control-Allow-Origin
header is returned.If the value of the
Origin
header in a user request does not match a value of theAccess-Control-Allow-Origin
header, theAccess-Control-Allow-Origin
header is not returned.
In the CDN console, the response header is set to Access-Control-Allow-Origin:http://example.com,https://aliyundoc.com
.
If the value of the
Origin
header in a user request is http://example.com
, CDN POPs returnAccess-Control-Allow-Origin:http://example.com
.If the value of the
Origin
header in a user request ishttp://aliyundoc.com
, CDN POPs do not returnAccess-Control-Allow-Origin
because the request uses HTTP but CDN POPs respond only to HTTPS requests in this case.If the value of the
Origin
header in a user request ishttps://aliyundoc.com
, CDN POPs returnAccess-Control-Allow-Origin:https://aliyundoc.com
.If the value of the
Origin
header in a user request ishttp://aliyun.com
, CDN POPs do not returnAccess-Control-Allow-Origin
because the domain names do not match.
Example 2
If you set the Access-Control-Allow-Origin
parameter to a wildcard domain name, CDN POPs check whether the value of the Origin
header in a user request matches the wildcard domain name.
In the CDN console, the response header is set to Access-Control-Allow-Origin:http://*.aliyundoc.com
.
If the value of the
Origin
header in a user request ishttp://demo.aliyundoc.com
, CDN POPs returnAccess-Control-Allow-Origin:http://demo.aliyundoc.com
.If the value of the
Origin
header in a user request ishttp://demo.example.com
, CDN POPs do not returnAccess-Control-Allow-Origin
because the domain names do not match.If the value of the
Origin
header in a user request ishttps://demo.aliyundoc.com
, CDN POPs do not returnAccess-Control-Allow-Origin
because the request uses HTTPS but CDN POPs respond only to HTTP requests in this case.