|
| 1 | +--- |
| 2 | +title: Use a custom domain with rule-based routing in Azure Container Apps (preview) |
| 3 | +description: Learn how to configure a custom domain with rule-based routing in Azure Container Apps. |
| 4 | +services: container-apps |
| 5 | +author: craigshoemaker |
| 6 | +ms.service: azure-container-apps |
| 7 | +ms.custom: devx-track-azurecli, devx-track-bicep |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 04/16/2025 |
| 10 | +ms.author: cshoe |
| 11 | +--- |
| 12 | + |
| 13 | +# Use a custom domain with rule-based routing in Azure Container Apps (preview) |
| 14 | + |
| 15 | +HTTP route configurations support custom domains, allowing you to route traffic from your own domain names to your container apps. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- An existing Azure Container Apps environment |
| 20 | +- A custom domain that you own |
| 21 | +- SSL certificate for your domain (unless using automatic certificates) |
| 22 | +- Container apps deployed to your environment |
| 23 | + |
| 24 | +> [!NOTE] |
| 25 | +> To set up your own custom domain and certificate, see [Custom domain names and free managed certificates](./custom-domains-managed-certificates.md). |
| 26 | +> |
| 27 | +> If you set `bindingType: "Auto"`, you don't need a `certificateId`. Container Apps creates the certificate for you based on the domain. |
| 28 | +
|
| 29 | +## Configuration |
| 30 | + |
| 31 | +Update your Container Apps YAML file to include a `customDomains` section. The following example demonstrates how to set up this configuration. |
| 32 | + |
| 33 | +```yml |
| 34 | +customDomains: |
| 35 | + - name: "<CUSTOM_DOMAIN_ENDPOINT>" |
| 36 | + certificateId: "<CERTIFICATE_ID>" |
| 37 | + bindingType: "SniEnabled" # Can also be "Disabled", "Auto" |
| 38 | +rules: |
| 39 | + - description: "Routing to App1" |
| 40 | + routes: |
| 41 | + - match: |
| 42 | + prefix: "/1" |
| 43 | + action: |
| 44 | + prefixRewrite: "/" |
| 45 | + targets: |
| 46 | + - containerApp: "<APP1_CONTAINER_APP_NAME>" |
| 47 | + - description: "Routing to App2" |
| 48 | + routes: |
| 49 | + - match: |
| 50 | + prefix: "/2" |
| 51 | + action: |
| 52 | + prefixRewrite: "/" |
| 53 | + - match: |
| 54 | + prefix: "/" |
| 55 | + targets: |
| 56 | + - containerApp: "<APP2_CONTAINER_APP_NAME>" |
| 57 | +``` |
| 58 | +
|
| 59 | +This configuration defines two routing rules for HTTP traffic. |
| 60 | +
|
| 61 | +| Property | Description | |
| 62 | +|---|---| |
| 63 | +| `customDomains.name` | The domain name you want to use (example: "app.contoso.com") | |
| 64 | +| `customDomains.certificateId` | Resource ID of your certificate (not needed with `bindingType: "Auto"`) | |
| 65 | +| `customDomains.bindingType` | How SSL is handled: "SniEnabled" (Server Name Indication), "Disabled" (HTTP only), or "Auto" (automatic certificate) | |
| 66 | +| `description` | Human-readable label for the rule | |
| 67 | +| `routes.match.prefix` | URL path prefix to match. For example, `/api`. | |
| 68 | +| `routes.action.prefixRewrite` | What to replace the matched prefix with before forwarding. | |
| 69 | +| `targets.containerApp` | The name of the container app where matching route request are sent. | |
| 70 | + |
| 71 | +These rules allow different paths on your custom domain to route to different container apps while also modifying the request path before it reaches the destination app. |
| 72 | + |
| 73 | +Other properties not listed that may affect your routes include the following. |
| 74 | + |
| 75 | +| Property | Description | |
| 76 | +|---|---| |
| 77 | +| `route.match.path` | Exact match path definition. | |
| 78 | +| `route.match.pathSeparatedPrefix` | Matches routes on '/' boundaries rather than any text. For example, if you set the value to `/product`, then it will match on `/product/1`, but not `/product1`. | |
| 79 | +| `route.match.caseSensitive` | Controls whether or not route patterns match with case sensitivity. | |
| 80 | +| `target.label` | Route to a specific labeled revision within a container app. | |
| 81 | +| `target.revision` | Route to a specific revision within a container app. | |
| 82 | + |
| 83 | +## Work with your custom domain |
| 84 | + |
| 85 | +Use the following commands to configure and use your custom domain mapping. |
| 86 | + |
| 87 | +Before running the following commands, make sure to replace placeholders surrounded by `<>` with your own values. |
| 88 | + |
| 89 | +### List route configurations |
| 90 | + |
| 91 | +Use `az containerapp env http-route-config list` to list all the defined route configurations. |
| 92 | + |
| 93 | +```azurecli |
| 94 | +az containerapp env http-route-config list \ |
| 95 | + --resource-group <RESOURCE_GROUP_NAME> \ |
| 96 | + --name <ENVIRONMENT_NAME> |
| 97 | +``` |
| 98 | + |
| 99 | +### Create a new route configuration |
| 100 | + |
| 101 | +Use `az containerapp env http-route-config create` to create a new route configuration. |
| 102 | + |
| 103 | +```azurecli |
| 104 | +az containerapp env http-route-config create \ |
| 105 | + --resource-group <RESOURCE_GROUP_NAME> \ |
| 106 | + --name <ENVIRONMENT_NAME> \ |
| 107 | + --http-route-config-name <CONFIGURATION_NAME> \ |
| 108 | + --yaml <CONTAINER_APPS_CONFIG_FILE> |
| 109 | +``` |
| 110 | + |
| 111 | +### Update a route configuration |
| 112 | + |
| 113 | +Use `az containerapp env http-route-config update` to update an existing route configuration. |
| 114 | + |
| 115 | +```azurecli |
| 116 | +az containerapp env http-route-config update \ |
| 117 | + --resource-group <RESOURCE_GROUP_NAME> \ |
| 118 | + --name <ENVIRONMENT_NAME> \ |
| 119 | + --http-route-config-name <CONFIGURATION_NAME> \ |
| 120 | + --yaml <CONTAINER_APPS_CONFIG_FILE> |
| 121 | +``` |
| 122 | + |
| 123 | +### Show a specific route configuration |
| 124 | + |
| 125 | +Use `az containerapp env http-route-config show` to view details of a route configuration. |
| 126 | + |
| 127 | +```azurecli |
| 128 | +az containerapp env http-route-config show \ |
| 129 | + --resource-group <RESOURCE_GROUP_NAME> \ |
| 130 | + --name <ENVIRONMENT_NAME> \ |
| 131 | + --http-route-config-name <CONFIGURATION_NAME> |
| 132 | +``` |
| 133 | + |
| 134 | +### Delete a route configuration |
| 135 | + |
| 136 | +Use `az containerapp env http-route-config delete` to remove a route configuration. |
| 137 | + |
| 138 | +```azurecli |
| 139 | +az containerapp env http-route-config delete \ |
| 140 | + --resource-group <RESOURCE_GROUP_NAME> \ |
| 141 | + --name <ENVIRONMENT_NAME> \ |
| 142 | + --http-route-config-name <CONFIGURATION_NAME> |
| 143 | +``` |
| 144 | + |
| 145 | +## Verify HTTP routing |
| 146 | + |
| 147 | +After configuring your custom domain with rule-based routing: |
| 148 | + |
| 149 | +1. Navigate to your custom domain in a browser. For example, `https://app.contoso.com/1`. |
| 150 | + |
| 151 | +1. Verify that the request is routed to the first container app. |
| 152 | + |
| 153 | +1. Change the path to `/2`. For example, `https://app.contoso.com/2`. |
| 154 | + |
| 155 | +1. Verify that the request is now routed to the second container app. |
| 156 | + |
| 157 | +## Related content |
| 158 | + |
| 159 | +- [Rule-based routing in Azure Container Apps](./rule-based-routing.md) |
| 160 | +- [Custom domain names and free managed certificates](./custom-domains-managed-certificates.md) |
| 161 | +- [Azure CLI reference for HTTP route configuration](/cli/azure/containerapp/env/http-route-config) |
| 162 | + |
| 163 | +> [!TIP] |
| 164 | +> Having issues? Let us know on GitHub by opening an issue in the [Azure Container Apps repo](https://github.com/microsoft/azure-container-apps). |
0 commit comments