Updated on 2024-12-10 GMT+08:00

HTTP Header Settings (Cross-origin Requests)

HTTP headers are part of an HTTP request or response message that define the operating parameters of an HTTP transaction.

Cross-origin resource sharing (CORS) is a mechanism that allows cross-origin access. When website A accesses resources on website B, a cross-origin request is sent. If website B does not allow website A to access the resources, a cross-domain problem occurs. In this case, you can configure HTTP header settings and add custom headers in response messages returned to the requester to implement functions such as CORS.

Precautions

  • Some headers cannot be set or deleted. For details, see Constraints.
  • You can add up to 10 HTTP response header rules.
  • HTTP header configuration is domain name-specific. When the configuration takes effect, the specified headers will be added to or removed from response messages for any resources under the entire domain. However, HTTP header configuration only affects the response behavior of the clients (browsers). They do not affect the cache behavior of CDN PoPs.
  • If a CORS rule is configured on the CDN console, synchronize it to your origin server. If your origin server is the domain name of an OBS bucket, configure CORS on OBS.

Supported Response Headers

Huawei Cloud CDN lets you customize the following different HTTP response headers:

  • Content-Disposition

    This header can start a download on clients and specify the name of the file to be downloaded.

    When a server sends a file to a browser, as long as the file format is supported (for example, TXT or JPG), the file is opened using the browser by default. You can use this header to treat the file as an attachment and let users save it with a specific file name.

  • Content-Language

    This header specifies the preferred language or language combination of the browser. Content can be customized for different users.

  • Access-Control-Allow-Origin

    This header carries the domain names that are allowed for CORS after server authentication. For a simple CORS request, the browser determines whether to return the requested content to the client based on this header. For a preflight request, the browser determines whether to initiate an actual CORS request to the server based on this header.

    To prevent cross-domain errors caused by browser cache, clear browser cache after configuring Access-Control-Allow-Origin.

  • Access-Control-Allow-Methods

    This header carries the methods that are allowed for CORS after server authentication. For a simple CORS request, the browser determines whether to return the requested content to the client based on this header. For a preflight request, the browser determines whether to initiate an actual CORS request to the server based on this header.

  • Access-Control-Max-Age

    This header determines how long the results of CORS preflight requests allowed by the server can be cached. The browser determines the TTL for preflight request results based on this header. As long as the TTL has not expired, the browser can determine whether to initiate a CORS request to the server. Once this TTL expires, the browser needs to send another preflight request to the server.

  • Access-Control-Expose-Headers

    This header specifies the response headers that the browser can expose to the client. You can use this header to define the response headers visible to the client. The following response headers are visible to the client by default: Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, and Pragma.

  • Custom

    If the preceding response headers cannot meet your needs, you can create response headers. A custom response header contains 1 to 100 characters, starting with a letter and consisting of letters, digits, and hyphens (-).

Procedure

  1. Log in to the Huawei Cloud console.
  2. In the navigation pane, choose Domains.
  3. In the domain list, click the target domain name or click Configure in the Operation column.
  4. Click the Advanced Settings tab.
  5. In the HTTP Headers area, click Edit. The Configure HTTP Headers dialog box is displayed.
    Figure 1 Configuring HTTP headers

  6. Click Add and select a response header operation from the drop-down list.

    Response Header Operation

    Description

    Set

    • If the header already exists in the response, the header value you configure will overwrite the original one.
    • If the header does not exist in the response, the header will be added to the response.

    Delete

    The header will be deleted from the response.

  7. Set the header parameter and value.

    Parameter

    Description

    Example Value

    Content-Disposition

    Starts a download on the client side and specifies the name of the file to be downloaded.

    Value requirements: Enter 1 to 1,000 characters. For a typical configuration, see the example on the right.

    attachment;filename=FileName.xls

    Content-Language

    Specifies the language of the response page of the client.

    Value requirements: Enter 1 to 1,000 characters. For a typical configuration, see the example on the right.

    zh-CN

    en-US

    Access-Control-Allow-Origin

    Specifies the foreign domain URLs (request sources) that are allowed to access the resource in CORS.

    Value requirements:

    • Enter a URL or up to 66 URLs.
    • Wildcard domain names are supported.
    • Enter up to 1,000 characters.
    • Separate URLs with commas (,).
    • Start with http:// or https://.
    • If this is set to *, no URLs are allowed after the wildcard (*).
    • Domain names with port numbers are supported.
    • The value can be null, which is case-insensitive.

    Example 1:

    https://www.example.com

    Example 2:

    *

    Example 3:

    https://www.example.com,https://www.example01.com,https://*.abc.com

    Access-Control-Allow-Methods

    Specifies the HTTP request methods that can be used in a CORS request.

    Value requirements: Enter 1 to 1,000 characters. Separate methods by commas (,).

    GET,POST,HEAD

    Access-Control-Max-Age

    Specifies how long to cache the results of CORS preflight requests on specific resources.

    Value requirements: This value is expressed in seconds and ranges from 0 to 1,000,000,000.

    86400

    Access-Control-Expose-Headers

    Specifies the response header information visible to the client for a CORS request.

    Value requirements: Enter 1 to 1,000 characters. Multiple headers can be configured at the same time. Separate them by commas (,).

    Content-Length,Content-Encoding

    Access-Control-Allow-Headers

    Specifies the fields that can be carried in a cross-domain request.

    Value requirements: Enter 1 to 1,000 characters. Multiple fields can be configured at the same time. Separate them by commas (,).

    X- Custom-Header

    Custom

    Specifies the custom response header for a CORS request. A response header starts with a letter and contains 1 to 100 characters, including letters, digits, and hyphens (-).

    Value requirements: Enter 1 to 1,000 characters, which can contain letters, digits, spaces, and the following special characters: .-_*#!&+|^~'"/:;,=@?<>

    NOTE:
    • If the custom parameter is Cache-Control, the value can be public, private, no-cache, no-store, no-transform, only-if-cached, proxy-revalidate, must-revalidate, immutable, max-age=***, stale-while-revalidate=***, s-maxage=***, stale-if-error=***, or min-fresh=*** (*** is a number). Enter up to 10 values and separate them by commas (,).
    • The value of the Cache-Control header may affect the PoP cache.

    x-testcdn

  8. Click OK.

Constraints

  • If your domain name has special configurations, Content-Type, Expires, or Cache-Control cannot be configured.
  • The following response headers can only be modified. Response Header Operation cannot be set to Delete for them.

    Content-Base

    Content-Type

    Server

    Content-Language

    Cache-Control

    Expires

  • CDN does not support the following response headers.

    a_dynamic

    upgrade

    content-md5

    accept-ranges

    meter

    content-range

    keep-alive

    www-authenticate

    date

    allow

    proxy-authenticate

    range

    set-cookie

    connection

    etag

    authentication-info

    content-encoding

    retry-after

    last-modified

    proxy-authorization

    error

    location

    content-length

    if-modified-since

    transfer-encoding

    content-location

    host