Alibaba Cloud CDN allows you to rewrite HTTP headers in HTTP response messages returned from an origin server. You can add, delete, change, and replace HTTP response headers based on your business requirements.

Background information

HTTP response headers are a component of the header section in response messages transmitted over HTTP. HTTP response headers bring specific parameters to clients.

If the requested resource is not cached on CDN nodes, the request is redirected to the origin server. Then, the origin server returns the requested resource to the CDN node. Alibaba Cloud CDN allows you to rewrite HTTP response headers from origin servers so that clients can identify response information more easily. For example, you can rewrite the value of the Content-Type header before it is returned to clients to ensure that the clients can parse the content retrieved from the origin server. If Content-Type is set to an invalid value, garbled text appears. In this case, the value of Content-Type must be rewritten on CDN nodes.

Responses to back-to-origin requests
Note
  • After an origin server receives a request from a CDN node, the origin server returns an HTTP message to the CDN node. A rewrite rule rewrites only HTTP headers in responses that are returned from an origin server. It does not rewrite HTTP headers in responses that are returned from CDN nodes to clients.
  • You cannot configure custom HTTP response headers for wildcard domain names.

Procedure

  1. Log on to the Alibaba Cloud CDN console.
  2. In the left-side navigation pane, click Domain Names.
  3. On the Domain Names page, find the domain name that you want to manage and click Manage in the Actions column of the domain name.
  4. In the management pane of the domain name, click Back-to-origin.
  5. Click the Custom Response Headers tab.
  6. Click Customize.
  7. Set the parameters in the Custom Response Headers dialog box.
    Notice If different operations are performed on the same response header at the same time, these operations have different priorities. The operations are prioritized in the following descending order: Replace > Add > Change or Delete. For example, if you perform the Add and Delete operations on the same response header at the same time, the response header is added and then deleted.
    • Parameters of the Add operationRewrite an HTTP response header
      Parameter Example Description
      Operation Add Adds a response header to response messages from an origin server.
      Response Header Custom Cache Response Headers

      You can use the default value, or select Custom Cache Response Headers from the Response Header drop-down list to specify a response header.

      Header Name x-code

      The name of the custom response header is x-code.

      Header Value key1

      You can specify one or more values for a response header. Separate values with commas (,).

      key1, key2
      Allow Duplicates Yes
      • Yes: You can add duplicate response headers. For example, x-code:key1 and x-code:key2 can coexist.
      • No: The latest header value overwrites the existing one that uses the same header name. For example, if you add x-code:key1 and then add x-code:key2, the final header key-value pair is x-code:key2.
    • Parameters of the Delete operationDelete
      Parameter Example Description
      Operation Delete Deletes the values of all specified responses headers. Duplicate responses headers are also deleted.
      Response Header Custom Cache Response Headers

      You can use the default value, or select Custom Cache Response Headers from the Response Header drop-down list to specify a response header.

      Header Name x-code

      The name of the custom response header is x-code.

    • Parameters of the Change operationChange
      Parameter Example Description
      Operation Change You can perform the Change operation only if no duplicate response headers exist.
      Response Header Custom Cache Response Headers

      You can use the default value, or select Custom Cache Response Headers from the Response Header drop-down list to specify a response header.

      Header Name x-code

      The name of the custom response header is x-code.

      Change Value To key1, key3

      You can specify one or more values for a response header. Separate values with commas (,).

    • Parameters of the Replace operationReplace
      Parameter Example Description
      Operation Replace You can perform the Replace operation only if no duplicate response headers exist.
      Response Header Custom Cache Response Headers

      You can use the default value, or select Custom Cache Response Headers from the Response Header drop-down list to specify a response header.

      Header Name x-code

      The name of the custom response header is x-code.

      Find key Allows you to use regular expressions to search for the value that you want to replace.
      Replace With abc Allows you to use regular expressions to replace matching values.
      Match Match All
      • Match All: All matching values are replaced. For example, if you use a regular expression to replace all the "key" in x-code:key1,key2,key3 with "abc", the key-value pair is changed tox-code:abc1,abc2,abc3.
      • Match the First Only: Only the first matching value is replaced. For example, if you use a regular expression to replace the first "key" in x-code:key1,key2,key3 with "abc", the key-value pair is changed to x-code:abc1,key2,key3.
  8. Click OK.