使用应用网关基于URL路由时出现404错误,路径规则配置求助
Hey John, let's troubleshoot that 404 error you're seeing with your Application Gateway path routing setup. The most likely culprit here is how the request path is being forwarded to your backend Web Apps—let's break this down step by step.
Core Issue
When you set up a path rule with -Paths "/web1", the Application Gateway forwards the full requested path to your backend Web App. That means when you hit http://<app_gateway_pub_ip>/web1, the gateway sends a request to http://webapp1.azurewebsites.net/web1 instead of the root path / of your Web App. Since your Web App doesn't have a /web1 endpoint, it returns a 404.
Fixes to Try
1. Add Path Rewriting to Strip the Prefix
You need to configure a path rewrite rule to strip the /web1 (or /web2) prefix before forwarding the request to the backend. Here's how to do it with PowerShell:
First, create a rewrite rule set and rewrite rules for both paths:
# Create rewrite rule for web1: replace /web1/* with /* $web1RewriteRule = New-AzureRmApplicationGatewayRewriteRuleConfig -Name "web1Rewrite" ` -UrlConfiguration @{ Path = "/{remainingpath}" PathPattern = "/web1/{remainingpath}" RewriteRuleSetType = "UrlRewrite" } # Create rewrite rule for web2: replace /web2/* with /* $web2RewriteRule = New-AzureRmApplicationGatewayRewriteRuleConfig -Name "web2Rewrite" ` -UrlConfiguration @{ Path = "/{remainingpath}" PathPattern = "/web2/{remainingpath}" RewriteRuleSetType = "UrlRewrite" } # Create a rewrite rule set containing both rules $rewriteRuleSet = New-AzureRmApplicationGatewayRewriteRuleSetConfig -Name "webAppRewriteSet" ` -RewriteRules $web1RewriteRule, $web2RewriteRule # Get your existing Application Gateway $appGateway = Get-AzureRmApplicationGateway -Name "YourAppGatewayName" -ResourceGroupName "YourResourceGroupName" # Add the rewrite rule set to the gateway $appGateway = Add-AzureRmApplicationGatewayRewriteRuleSet -ApplicationGateway $appGateway -RewriteRuleSet $rewriteRuleSet # Update your path rules to include the rewrite rule set, and use wildcard paths $web1PathRule = New-AzureRmApplicationGatewayPathRuleConfig -Name "web1pathrule" ` -Paths "/web1/*" ` -BackendAddressPool $regbackendpool ` -BackendHttpSettings $poolSetting ` -RewriteRuleSet $rewriteRuleSet $web2PathRule = New-AzureRmApplicationGatewayPathRuleConfig -Name "web2pathrule" ` -Paths "/web2/*" ` -BackendAddressPool $web2backendpool ` # Replace with your web2 backend pool -BackendHttpSettings $poolSetting ` -RewriteRuleSet $rewriteRuleSet # Update the gateway's URL path map with the new path rules $urlPathMap = Get-AzureRmApplicationGatewayUrlPathMapConfig -ApplicationGateway $appGateway -Name "YourPathMapName" $urlPathMap = Set-AzureRmApplicationGatewayUrlPathMapConfig -ApplicationGateway $appGateway ` -Name "YourPathMapName" ` -PathRules $web1PathRule, $web2PathRule ` -DefaultBackendAddressPool $defaultPool ` # Replace with your default pool if needed -DefaultBackendHttpSettings $poolSetting # Save the changes to the gateway Set-AzureRmApplicationGateway -ApplicationGateway $appGateway
2. Use Wildcard Paths in Your Rules
Notice we changed -Paths "/web1" to -Paths "/web1/*"—this ensures all subpaths under /web1 (like /web1/about) are matched and rewritten, not just the exact /web1 URL. Without the wildcard, only the root of /web1 would be routed, leading to 404s for any subpages.
3. Verify Backend Health
Double-check that your Application Gateway's backend pools are showing as healthy. You can do this in the Azure Portal:
- Go to your Application Gateway → Backend Health
- Ensure both
webapp1.azurewebsites.netandwebapp2.azurewebsites.netare marked as "Healthy"
If they're unhealthy, fix connectivity issues first (check NSGs, Web App access restrictions, etc.)
4. Confirm Web App Routing
Make sure your Web Apps are configured to serve content from the root path (/). For example, if you're using an ASP.NET Core app, verify your Startup.cs has correct routing setup, or if it's a static site, ensure your index file is in the root directory.
Final Check
After applying these changes, test accessing http://<app_gateway_pub_ip>/web1 again—the gateway should now rewrite the path to / and forward it to your Web App, resolving the 404 error.
内容的提问来源于stack exchange,提问作者John

