Azure Web App虚拟应用能否绑定独立自定义域名?实现方案咨询
Absolutely! You don’t need to create separate Azure Web Apps for this scenario—you can map main.mydomain.com to your first virtual app (app1) and api.mydomain.com to the second (app2) using a combination of domain binding and URL rewriting. Here’s a step-by-step breakdown:
Step 1: Bind your custom subdomains to the main Web App
First, you need to link both subdomains to your parent Web App since virtual apps are nested under it:
- Log into the Azure Portal and navigate to your main Web App.
- From the left menu, select Custom domains.
- Click Add custom domain, enter
main.mydomain.com, and follow the prompts to configure your DNS records (use a CNAME record pointing to your Web App's default.azurewebsites.netdomain). - Repeat this process for
api.mydomain.com. - Verify both domains in Azure once the DNS changes propagate (this can take a few minutes to hours).
Step 2: Configure URL rewrite rules to route to virtual apps
Azure Web Apps use IIS URL Rewrite to route traffic to virtual apps without exposing their subpaths. You’ll need to add rules to your main Web App’s web.config file:
- Access your Web App’s Kudu console via Advanced Tools > Go in the Azure Portal, or edit the
web.configfile directly in your deployment pipeline. - Add the following rewrite rules inside the
<system.webServer>section of yourweb.config:<rewrite> <rules> <!-- Route all traffic from main.mydomain.com to /app1 --> <rule name="MainDomainRoute" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^main\.mydomain\.com$" /> </conditions> <action type="Rewrite" url="/app1/{R:1}" /> </rule> <!-- Route all traffic from api.mydomain.com to /app2 --> <rule name="ApiDomainRoute" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^api\.mydomain\.com$" /> </conditions> <action type="Rewrite" url="/app2/{R:1}" /> </rule> </rules> </rewrite> - Save the
web.configfile—Azure will automatically apply the changes.
Step 3: (Optional) Set up SSL bindings
If you want to use HTTPS for your custom domains:
- In the Custom domains section of your Web App, select each subdomain and click Add binding.
- Choose an existing SSL certificate (or upload a new one) and select the appropriate SSL type (SNI SSL is recommended for multiple domains).
Step 4: Test the setup
Wait for DNS and SSL changes to take effect, then visit main.mydomain.com—you should see your app1 content, and api.mydomain.com should load app2. If you run into issues, double-check:
- DNS records are correctly pointing to your Web App's default domain.
- The rewrite rules in
web.configmatch your virtual app paths (e.g., if your virtual app is named/app1, the rule should use that path). - Virtual apps are properly configured in your Web App’s Configuration > Path mappings section.
内容的提问来源于stack exchange,提问作者Keshavdas M

