This topic describes the typical scenarios of custom domain names. This topic also describes how to bind a custom domain name to a web application and enable Alibaba Cloud CDN acceleration for the bound custom domain name in the Function Compute console.

Typical scenarios

Function Compute allows you to create HTTP functions. Only HTTP functions can be triggered by HTTP requests. An HTTP function can be considered as a web application that can process HTTP requests and return the results to the caller. In the following sample scenarios, you must bind a custom domain name to a web application:
  • You have created a web application and migrated the web application to Function Compute, and want users to access the web application by using a fixed domain name. In this scenario, you can bind a custom domain name to the web application in the Function Compute console so that users can use the domain name to access the web application.
  • You have created a web application in the Function Compute console and can access it by using the default URL <account_id>.<region_id>.fc.aliyuncs.com/<version>/proxy/<serviceName>/<functionName>/[action?queries] provided by Function Compute. You may need to change the URL of the web application. To prevent the impact on the access of users to the web application, you can bind a custom domain name to the web application so that the users can use the domain name to access the web application.

Prerequisites

An HTTP function is created. Requests sent from a custom domain name can trigger only HTTP functions. Therefore, you must first create an HTTP function. For more information, see Create a function.

Procedure

Custom domain name

Step 1: Apply for an ICP filing for a custom domain name

Apply for an Internet Content Provider (ICP) filing for your custom domain name in the Alibaba Cloud ICP Filing system. For more information, see ICP filing application overview.

Step 2: Configure domain name resolution

Configure domain name resolution to resolve the custom domain name to the Function Compute endpoint of the corresponding region. For more information, see Quick Start. You can resolve the domain name to both the public and internal endpoints. If you resolve the domain name to a public endpoint, the domain name is accessed over the Internet. If you resolve the domain name to an internal endpoint, the domain name is accessed over the internal network.

Note When you configure domain name resolution to resolve the custom domain name to the Function Compute endpoint, you configure the canonical name (CNAME) of the custom domain name to the Function Compute endpoint. The endpoint is in one of the following formats:
  • Internal endpoint: <account_id>.<region_id>-internal.fc.aliyuncs.com, where account_id specifies the ID of your Alibaba Cloud account. For example, if your custom domain name is example.com, the ID of your Alibaba Cloud account is 164901546557****, and the region is China (Shanghai), the internal endpoint is 164901546557****.cn-shanghai-internal.fc.aliyuncs.com.
  • Public endpoint: <account_id>.<region_id>.fc.aliyuncs.com, where account_id specifies the ID of your Alibaba Cloud account. For example, if your custom domain name is example.com, the ID of your Alibaba Cloud account is 164901546557****, and the region is China (Shanghai), the public endpoint is 164901546557****.cn-shanghai.fc.aliyuncs.com.

Step 3: Add the custom domain name

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, choose Advanced Features > Domain Names.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Domain Names page, click Add Custom Domain Name.
  5. On the Add Custom Domain Name page, set the parameters as required and click OK.
    create-domain
    Parameter Operation
    Domain Name Enter the custom domain name that has obtained the ICP filing in the Alibaba Cloud ICP Filing system or whose ICP filing information includes Alibaba Cloud as a service provider.
    HTTPS Set this parameter to Enable or Disable as required to allow or disallow the custom domain name to be accessed over HTTPS. Valid values:
    • Enable: allows the custom domain name to be accessed over HTTPS. If you set the HTTPS parameter to Enable, you must set the Certificate Type parameter. Valid values:
      • Alibaba Cloud SSL Certificate: Select your Alibaba Cloud SSL certificate from the Certificate Name drop-down list. If the drop-down list is empty, you have not purchased an Alibaba Cloud SSL certificate. You can log on to the SSL Certificates Service console to purchase an Alibaba Cloud SSL certificate.
      • Manual Upload: Set the Certificate Name, PEM Certificate Content, and PEM Certificate Key parameters.
        Note The size of the certificate to be uploaded cannot exceed 20 KB. The size of the private key cannot exceed 4 KB.
    • Disable: disallows the custom domain name to be accessed over HTTPS.
    CDN Acceleration Set this parameter to Enable or Disable as required to enable or disable CDN acceleration for the custom domain name. If you enable CDN acceleration for the custom domain name, users read the required content by using the CDN-accelerated domain name with high efficiency.
    • Enable: enables CDN acceleration. If you set the CDN Acceleration parameter to Enable, you must enter a domain name for CDN in the CDN Accelerated Domain Name field. Then, log on to the CDN console and configure a CNAME for the domain name for CDN. For more information, see Step 4: Enable CDN acceleration (optional).
    • Disable: disables CDN acceleration.
    Route Configure the relationship between paths and functions. This way, requests from different paths can trigger different functions. You must set the following fields:
    • Path: the path from which a request can trigger the specified function in the specified service.
    • Service Name: the name of the service to which the specified function belongs.
    • Function Name: the name of the function triggered by a request from the specified path.
    • Version or Alias: the version or alias of the service to which the function triggered by a request from the specified path belongs.

    You can configure multiple routes as needed. For more information, see Routing rules.

Step 4: Enable CDN acceleration (optional)

After you bind a custom domain name to a web application, you can use the custom domain name as the origin domain name, add a domain name for CDN to the custom domain name, and then configure a CNAME for the domain name for CDN. This way, CDN acceleration is enabled for the custom domain name. An application deployed in Function Compute is used as the origin, and source content is published to edge nodes so that users can read the required content with high efficiency. CDN acceleration effectively reduces access latency and improves service quality. For more information about CDN, see CDN documentation.

Notice If you enable CDN acceleration, you are charged Internet traffic fees. For more information, see Billing.

Method 1: Add a domain name for CDN in the Function Compute console

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, choose Advanced Features > Domain Names.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Domain Names page, find the domain name for which you want to enable CDN acceleration and click Modify in the Actions column.
  5. On the Modify Custom Domain Name page, set the CDN Acceleration parameter to Enable, enter a domain name for CDN in the CDN Accelerated Domain Name field, and then click Save.
    Note You can specify multiple domain names for CDN for one custom domain name.
  6. Log on to the CDN console. In the left-side navigation pane, click Domain Names. On the Domain Names page, you can view the domain name for CDN that you created.

Method 2: Add a domain name for CDN in the CDN console

For information about how to enable CDN acceleration in the CDN console, see Add a domain name.

When you add a domain name for CDN, select Function Compute Domain as Origin Info. Then, select the region where your Function Compute service resides and the custom domain name that you added in the Function Compute console.

After you add the domain name for CDN, you can use the Function Compute console to verify that CDN acceleration is enabled for your custom domain name and that the specified domain name for CDN added in the CDN console is bound to your custom domain name. To verify the results, perform the following operations:

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, choose Advanced Features > Domain Names.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Domain Names page, find the specified domain name and click Modify in the Actions column.
  5. On the Modify Custom Domain Name page, you can verify that the setting of the domain name for CDN is synchronized from the CDN console.
    accelerate-domain2
After you add the domain name for CDN, you must configure a CNAME for the domain name for CDN. For more information, see Add a CNAME record for a domain name.
Note The CNAME is in the following format: Domain name for CDN.w.alikunlun.com. Example: example.aliyundoc.com.w.alikunlun.com.

Verify the results

After you add the custom domain name or the domain name for CDN, you can use one of the following methods to access the custom domain name or the domain name for CDN and verify the result:

  • Method 1: Run the curl URL command, such as curl example.com/login.
  • Method 2: Use a browser.

    Enter the request URL in the address bar of a browser and press the ENTER key to check whether the specified function is invoked.

Routing rules

You must configure the relationship between paths and functions when you bind a custom domain name. This way, requests from different paths can trigger different functions. Function Compute supports exact matches and fuzzy matches for paths. These matching rules are implemented in the following way:
  • Exact match: A function is triggered only when the path of the request is exactly the same as the specified path.

    For example, you have created a route whose path is /a, the corresponding service is s1, the corresponding function is f1, and the corresponding version is 1. Only requests from exactly the /a path can trigger the f1 function of version 1. Requests from the /a/ path cannot trigger the f1 function of version 1.

  • Fuzzy match: You can append an asterisk (*) as a wildcard to a path.

    For example, you have created a route whose path is /login/*, the corresponding service is s2, the corresponding function is f2, and the corresponding version is 1. Requests from all paths that begin with /login/, such as /login/a and /login/b/c/d, can trigger the f2 function of version 1.

Note
  • If multiple routes are configured for one custom domain name, an exact match takes precedence over a fuzzy match.
  • For a request URL that has multiple fuzzy matches, the longest prefix match (LPM) rule determines the path to use.

    For example, the /login/a/* path and the /login/* path are configured for the custom domain name example.com, and the request URL is example.com/login/a/b. The request URL matches both configured paths. However, the /login/a/* path is used based on the LPM rule.

Examples

For example, the custom domain name is example.com. The following table describes five routing rules configured based on the steps described in this topic.
Routing rule Path Service name Function name Version
Routing rule 1 / s1 f1 1
Routing rule 2 /* s2 f2 2
Routing rule 3 /login s3 f3 3
Routing rule 4 /login/a s4 f4 4
Routing rule 5 /login/* s5 f5 5
The following table describes the final matches.
Request URL Matched service name Matched function name Matched version Matched path
example.com s1 f1 1 /
example.com/user s2 f2 2 /*
example.com/login s3 f3 3 /login
example.com/login/a s4 f4 4 /login/a
example.com/login/a/b s5 f5 5 /login/*
example.com/login/b s5 f5 5 /login/*

Troubleshooting

If an error occurs when you bind a custom domain name, the server returns an error message. The following table describes common error codes to help you identify and resolve issues with high efficiency.

Error code HTTP status code Error message Cause
DomainNameAlreadyExists 409 domain name '%s' already exists The error message returned because the domain name that you want to bind already exists.
DomainNameNotFound 404 domain name '%s' does not exist The error message returned because the domain name that you want to query does not exist.
InvalidICPLicense 400 domain name '%s' has not got ICP license, or the ICP license does not belong to Aliyun The error message returned because the domain name has not obtained an ICP filing or its ICP filing information does not include Alibaba Cloud as a service provider.
DomainNameNotResolved 400 domain name '%s' has not been resolved to your FC endpoint, the expected endpoint is '%s' The error message returned because no CNAME has been configured for the domain name to point to the specified endpoint. You can check the CNAME settings by running the dig command or logging on to the Domain Name System (DNS) server.
DomainRouteNotFound 404 no route found in domain '%s' for path '%s' The error message returned because no to-be-triggered function is configured for the specified path.
TriggerNotFound 404 trigger 'http' does not exist in service '%s' and function '%s' The error message returned because no HTTP trigger is configured for the function bound to the custom domain name.

If your problem persists, contact Function Compute engineers.