This topic describes the typical application scenarios of a custom domain name and how to bind a custom domain name to a Web application in the Function Compute console and set the Alibaba Cloud Content Delivery Network acceleration feature for the bound custom domain name.

Scenarios

Function service support for creating HTTP functions. Only HTTP functions can be triggered by HTTP requests. An HTTP function can be regarded as a Web application that can process HTTP requests and return the processing results to the caller. In the following sample scenarios, you must bind a custom domain name to a web application:
  • You create a Web application and migrate the application to Function service. You want users to access the application through a fixed domain name. You can bind a custom domain name to the web application in the Function Compute console to access the application through a fixed domain name.
  • You build a Web application by using the Function Compute console. You can <account_id>.<region_id>.fc.aliyuncs.com/<version>/proxy/<serviceName>/<functionName>/[action?queries] access the application by using the default address provided by the Function service. In the actual operation process, there is a need to change the access address of the Web application. In order not to affect the user's use, you can bind a custom domain name to the Web application to access the application through a fixed domain name.

Prerequisites

Create an HTTP function. Requests made through a custom domain name can only trigger HTTP function execution. Therefore, you must first create an HTTP function. For more information, see Create a function.

Procedure

Custom domain name

Step 1: ICP filing for a domain name

Log on to 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 the endpoints of the corresponding region of your Function Compute. For more information, see Add an A record to a website domain. It can be resolved to both public and internal endpoints. If you resolve to a public endpoint, the domain name is accessed over the Internet. If you resolve to an internal endpoint, the domain name is accessed over the internal network.

Note Configuring domain name resolution to Function Compute endpoints means configuring the CNAME of a custom domain name to Function Compute endpoints. The format of endpoints is described as follows:
  • Internal Endpoint: The format is <account_id>.<region_id>-internal.fc.aliyuncs.com, where the account_id refers to the ID of your Alibaba Cloud account (primary account). For example, if your custom domain name is example.com, your Alibaba Cloud account (primary account) ID is 164901546557****, and the region is China (Shanghai), the internal endpoint is 164901546557****.cn-shanghai-internal.fc.aliyuncs.com.
  • Public Endpoint: The format is <account_id>.<region_id>.fc.aliyuncs.com, where the account_id refers to the ID of your Alibaba Cloud account (primary account). For example, if your custom domain name is example.com, your Alibaba Cloud account (primary account) ID is 164901546557****, and the region is China (Shanghai), the public endpoint is 164901546557****.cn-shanghai.fc.aliyuncs.com.

Step 3: Add a 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. In the Add Custom Domain Name dialog box, set the parameters and click Create.
    create-domain
    Parameter Operation
    Domain Name Enter a custom domain name that has obtained an ICP filing in the Alibaba Cloud ICP Filing system or whose ICP filing information includes Alibaba Cloud as a service provider.
    HTTPS Enable or disable HTTPS access to custom domain names as needed. Valid values:
    • Enable: enables HTTPS access to custom domain names. You must select Certificate Type. Valid values:
      • Alibaba Cloud SSL Certificate: Select your Alibaba Cloud SSL Certificates Service. If the drop-down list is empty, you have not purchased an Alibaba Cloud SSL Certificates Service. You can log on to the SSL console to purchase it.
      • Manual Upload: Enter Certificate Name and enter PEM Certificate Content and PEM Certificate Key.
        Note The size of the uploaded certificate cannot exceed 20 KB. The size of the private key cannot exceed 4 KB.
    • Disable: Disable the HTTPS access function.
    Alibaba Cloud Content Delivery Network acceleration Enable or disable the Alibaba Cloud Content Delivery Network acceleration switch as required. After you set the Alibaba Cloud Content Delivery Network acceleration feature for a domain name, the end user quickly reads the required content by Alibaba Cloud Content Delivery Network the accelerated domain name.
    route settings 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/Alias: the version or alias of the specified service to which the function triggered by a request from the specified path belongs.

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

Step 4: Enable Alibaba Cloud Content Delivery Network acceleration (optional)

After you bind a custom domain name to a Web application, you can add an accelerated domain name to the custom domain name as the origin domain name. Then, configure a CNAME for the accelerated domain name, that is, set the Alibaba Cloud Content Delivery Network acceleration feature for the domain name. Use applications deployed in the Function service as the origin site and publish the source content to edge nodes. This enables end user to quickly read the required content, effectively reducing access latency and improving service quality. For more information about Alibaba Cloud Content Delivery Network, see Alibaba Cloud Content Delivery Network Help Documentation .

Notice The Alibaba Cloud Content Delivery Network acceleration feature consumes Internet traffic. You are charged for the traffic. For more information, see Billing.

Method 1: Use the Function Compute console to add an accelerated 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. In the domain name list, find the target domain name and click Edit in the Actions column.
  5. On the Edit Custom Domain Name page, enable the Alibaba Cloud Content Delivery Network acceleration feature, enter a custom Alibaba Cloud Content Delivery Network accelerated domain name, and then click Save.
    Note You can configure multiple domain names to accelerate for one custom domain name.
  6. Log on to the Alibaba Cloud Content Delivery Network console. On the Domain Names page, you can view the newly created accelerated domain name in the domain name list.

Method 2: Use the Alibaba Cloud Content Delivery Network console to add an accelerated domain name

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

When setting the origin information, select the Function Compute domain name, and then select the region where the target Function Compute service resides and the custom domain name that has been created.

After the accelerated domain name is added, you can see that the Alibaba Cloud Content Delivery Network function switch is turned on in the target custom domain name in the Function Compute console and the accelerated domain name added in the Alibaba Cloud Content Delivery Network console has been bound. 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. In the domain name list, find the target domain name and click Edit in the Actions column.
  5. On the Edit Custom Domain Name page, you can view the Alibaba Cloud Content Delivery Network accelerated domain configurations synchronized from the Alibaba Cloud Content Delivery Network console.
    accelerate-domain2
After you add a Alibaba Cloud Content Delivery Network accelerated domain name to a custom domain name, you must configure a CNAME for the accelerated domain name. For more information, see Add a CNAME record for a domain name.
Note The CNAME is in the accelerated domain. w.alikunlun.com format, such as example.aliyundoc.com.w.alikunlun.com.

Check the result

After the custom domain name or Alibaba Cloud Content Delivery Network accelerated domain name is set, you can use the following methods to access the custom domain name or Alibaba Cloud Content Delivery Network accelerated domain name for testing.

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

    Enter the request URL in a web 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. The Function service supports exact match and fuzzy match. The specific rules are as follows:
  • Exact match: A function is triggered only when the path of the request is exactly the same as the specified path.

    Assume that 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.

    Assume that 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.

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

Examples

Assume that the custom domain name is example.com. According to the procedure in this topic, the following five routing rules are set.
Routing Rule Path Service Function 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/*

Error diagnostics

If an error occurs during the binding of a custom domain name, the server returns an error message. The following table lists common error codes to help you quickly locate and resolve the problem.

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 create 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 been filed with an Internet Content Provider (ICP) license 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 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 us.