πŸ—ΊοΈ GCP URL Maps Configuration

πŸ’‘ Key Concept: URL Maps are the traffic routing engine of GCP load balancers. They define how requests are routed to different backend services based on URL paths, host headers, HTTP methods, and other request attributes. Think of them as the "brain" that decides where each request should go.

πŸ—οΈ URL Map Architecture

graph TB Client[Client Request
example.com/api/users] --> FR[Forwarding Rule] FR --> TP[Target Proxy] TP --> UM[URL Map
Traffic Router] UM --> DR{Default Rule} UM --> PR1{Path Rule 1
/api/*} UM --> PR2{Path Rule 2
/static/*} UM --> HR{Host Rule
api.example.com} DR --> BS1[Backend Service
Web Servers] PR1 --> BS2[Backend Service
API Servers] PR2 --> BS3[Backend Service
CDN/Static] HR --> BS4[Backend Service
API Gateway] BS1 --> WEB[Web Instances] BS2 --> API[API Instances] BS3 --> CDN[Static Content] BS4 --> GW[Gateway Instances] classDef urlMapComponent fill:#ea4335,stroke:#333,stroke-width:2px,color:#fff classDef backendComponent fill:#34a853,stroke:#333,stroke-width:2px,color:#fff classDef ruleComponent fill:#fbbc04,stroke:#333,stroke-width:2px,color:#333 class UM urlMapComponent class BS1,BS2,BS3,BS4 backendComponent class DR,PR1,PR2,HR ruleComponent

🎯 URL Map Components

Component Purpose Example Priority
Default Service Catches all unmatched requests Fallback to main website Lowest (always last)
Host Rules Route based on hostname/domain api.example.com β†’ API backend Highest
Path Matchers Define routing rules for paths /api/* β†’ API service Medium
Path Rules Specific path routing within matcher /api/v1/* vs /api/v2/* Medium
Route Rules Advanced routing with conditions Header-based routing High

πŸš€ Basic URL Map Creation

Simple URL Map (Default Service Only)

# Create basic URL map with default service gcloud compute url-maps create basic-url-map \ --default-service=web-backend-service \ --global # Describe the URL map gcloud compute url-maps describe basic-url-map --global
πŸ“ Parameters:
  • --default-service: Backend service that handles all unmatched requests
  • --global: Creates global URL map (vs --region for regional)
  • Basic use case: Single application with no path-based routing needed

πŸ›£οΈ Path-Based Routing

Path Matcher Configuration

# Create URL map with path-based routing gcloud compute url-maps create path-based-url-map \ --default-service=web-backend-service \ --global # Add path matcher for API routes gcloud compute url-maps add-path-matcher path-based-url-map \ --path-matcher-name=api-matcher \ --default-service=web-backend-service \ --path-rules="/api/*=api-backend-service,/api/v1/*=api-v1-backend-service" \ --global # Add path matcher for static content gcloud compute url-maps add-path-matcher path-based-url-map \ --path-matcher-name=static-matcher \ --default-service=web-backend-service \ --path-rules="/static/*=static-backend-service,/images/*=image-backend-service" \ --global
πŸ“ Path Matcher Parameters:
  • --path-matcher-name: Unique identifier for the path matcher
  • --path-rules: Comma-separated "path=service" pairs
  • Path patterns: Support wildcards (*) and specific paths
  • Order matters: More specific paths should come first

🌐 Host-Based Routing

Multi-Domain Configuration

# Create URL map for multiple domains gcloud compute url-maps create multi-domain-url-map \ --default-service=main-website-service \ --global # Add host rule for API subdomain gcloud compute url-maps add-host-rule multi-domain-url-map \ --hosts="api.example.com" \ --path-matcher=api-path-matcher \ --global # Add host rule for admin subdomain gcloud compute url-maps add-host-rule multi-domain-url-map \ --hosts="admin.example.com,admin.mycompany.com" \ --path-matcher=admin-path-matcher \ --global # Create the path matchers for each host gcloud compute url-maps add-path-matcher multi-domain-url-map \ --path-matcher-name=api-path-matcher \ --default-service=api-backend-service \ --path-rules="/v1/*=api-v1-service,/v2/*=api-v2-service" \ --global gcloud compute url-maps add-path-matcher multi-domain-url-map \ --path-matcher-name=admin-path-matcher \ --default-service=admin-backend-service \ --path-rules="/dashboard/*=admin-dashboard-service" \ --global
πŸ“ Host Rule Parameters:
  • --hosts: Comma-separated list of hostnames/domains
  • --path-matcher: References a path matcher for this host
  • Wildcards: Support subdomain wildcards like *.example.com
  • Multiple domains: Same path matcher can serve multiple hosts

πŸ”„ Traffic Routing Examples

graph LR subgraph "Incoming Requests" R1[example.com/] R2[example.com/api/users] R3[api.example.com/v1/data] R4[example.com/static/logo.png] R5[admin.example.com/dashboard] end subgraph "URL Map Processing" UM[URL Map Rules] end subgraph "Backend Services" BS1[Web Service
Main Website] BS2[API Service
Application API] BS3[API V1 Service
Legacy API] BS4[Static Service
CDN/Assets] BS5[Admin Service
Management Panel] end R1 --> UM R2 --> UM R3 --> UM R4 --> UM R5 --> UM UM --> BS1 UM --> BS2 UM --> BS3 UM --> BS4 UM --> BS5 style R1 fill:#e1f5fe style R2 fill:#f3e5f5 style R3 fill:#e8f5e8 style R4 fill:#fff3e0 style R5 fill:#fce4ec

⚑ Advanced Routing Features

Header-Based Routing

# Create URL map with header-based routing using route rules gcloud compute url-maps create advanced-url-map \ --default-service=production-service \ --global # Import complex routing configuration from YAML file cat > url-map-config.yaml << 'EOF' name: advanced-url-map defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT/global/backendServices/production-service hostRules: - hosts: - api.example.com pathMatcher: api-matcher pathMatchers: - name: api-matcher defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT/global/backendServices/api-service routeRules: - priority: 1 matchRules: - prefixMatch: "/api/v1/" headerMatches: - headerName: "X-API-Version" exactMatch: "beta" service: https://www.googleapis.com/compute/v1/projects/PROJECT/global/backendServices/beta-api-service - priority: 2 matchRules: - prefixMatch: "/api/" queryParameterMatches: - name: "version" exactMatch: "2.0" service: https://www.googleapis.com/compute/v1/projects/PROJECT/global/backendServices/api-v2-service pathRules: - paths: - "/api/legacy/*" service: https://www.googleapis.com/compute/v1/projects/PROJECT/global/backendServices/legacy-api-service EOF # Import the configuration gcloud compute url-maps import advanced-url-map \ --source=url-map-config.yaml \ --global

Traffic Splitting & A/B Testing

# Create URL map for traffic splitting cat > traffic-split-config.yaml << 'EOF' name: traffic-split-url-map defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT/global/backendServices/production-service pathMatchers: - name: split-matcher defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT/global/backendServices/production-service routeRules: - priority: 1 matchRules: - prefixMatch: "/app/" routeAction: weightedBackendServices: - backendService: https://www.googleapis.com/compute/v1/projects/PROJECT/global/backendServices/production-service weight: 80 - backendService: https://www.googleapis.com/compute/v1/projects/PROJECT/global/backendServices/canary-service weight: 20 hostRules: - hosts: - app.example.com pathMatcher: split-matcher EOF gcloud compute url-maps import traffic-split-url-map \ --source=traffic-split-config.yaml \ --global
πŸ“ Advanced Routing Features:
  • Route Rules: More flexible than simple path rules
  • Header Matching: Route based on HTTP headers
  • Query Parameters: Route based on URL parameters
  • Traffic Splitting: A/B testing with weighted backends
  • Priority: Lower numbers = higher priority

πŸ”§ URL Map Management

View and Update URL Maps

# List all URL maps gcloud compute url-maps list # Describe specific URL map gcloud compute url-maps describe my-url-map --global # Export URL map to file for editing gcloud compute url-maps export my-url-map \ --destination=my-url-map.yaml \ --global # Update URL map from file gcloud compute url-maps import my-url-map \ --source=my-url-map-updated.yaml \ --global # Remove path matcher gcloud compute url-maps remove-path-matcher my-url-map \ --path-matcher-name=old-matcher \ --global # Remove host rule gcloud compute url-maps remove-host-rule my-url-map \ --host=old.example.com \ --global

πŸ“Š URL Map Validation

Testing URL Map Configuration

# Validate URL map configuration gcloud compute url-maps validate my-url-map \ --global # Test specific request routing gcloud compute url-maps test-route my-url-map \ --host=api.example.com \ --path="/api/v1/users" \ --headers="X-API-Version=beta" \ --global
Example Test Output: matchedUrlMapRule: service: https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/beta-api-service matchRule: prefixMatch: "/api/v1/" headerMatches: - headerName: "X-API-Version" exactMatch: "beta" priority: 1

🎨 Complex Routing Scenarios

E-commerce Platform Example

# Complete e-commerce URL map setup gcloud compute url-maps create ecommerce-url-map \ --default-service=storefront-service \ --global # API subdomain routing gcloud compute url-maps add-host-rule ecommerce-url-map \ --hosts="api.mystore.com" \ --path-matcher=api-matcher \ --global # Admin subdomain routing gcloud compute url-maps add-host-rule ecommerce-url-map \ --hosts="admin.mystore.com" \ --path-matcher=admin-matcher \ --global # CDN subdomain for static assets gcloud compute url-maps add-host-rule ecommerce-url-map \ --hosts="cdn.mystore.com,static.mystore.com" \ --path-matcher=cdn-matcher \ --global # API path matcher with versioning gcloud compute url-maps add-path-matcher ecommerce-url-map \ --path-matcher-name=api-matcher \ --default-service=api-service \ --path-rules="/v1/*=api-v1-service,/v2/*=api-v2-service,/auth/*=auth-service,/payments/*=payment-service" \ --global # Admin path matcher gcloud compute url-maps add-path-matcher ecommerce-url-map \ --path-matcher-name=admin-matcher \ --default-service=admin-service \ --path-rules="/dashboard/*=admin-dashboard-service,/reports/*=analytics-service" \ --global # CDN path matcher for static content gcloud compute url-maps add-path-matcher ecommerce-url-map \ --path-matcher-name=cdn-matcher \ --default-service=cdn-service \ --path-rules="/images/*=image-service,/css/*=static-service,/js/*=static-service" \ --global # Main site path routing for different sections gcloud compute url-maps add-path-matcher ecommerce-url-map \ --path-matcher-name=main-matcher \ --default-service=storefront-service \ --path-rules="/products/*=catalog-service,/cart/*=cart-service,/checkout/*=checkout-service,/api/*=api-service" \ --global # Apply main matcher to the main domain gcloud compute url-maps add-host-rule ecommerce-url-map \ --hosts="mystore.com,www.mystore.com" \ --path-matcher=main-matcher \ --global

πŸ“ˆ URL Map Routing Flow

sequenceDiagram participant Client participant LB as Load Balancer participant UM as URL Map participant HostRule as Host Rules participant PathMatcher as Path Matcher participant RouteRule as Route Rules participant Backend as Backend Service Client->>LB: Request: api.example.com/v1/users?version=beta LB->>UM: Route Request UM->>HostRule: Check Host: api.example.com HostRule->>PathMatcher: Match Found β†’ api-matcher PathMatcher->>RouteRule: Check Route Rules (Priority Order) alt Route Rule Match Found RouteRule->>Backend: Route to specific backend Note over RouteRule: Headers, Query Params, etc. else No Route Rule Match PathMatcher->>Backend: Use Path Rules Note over PathMatcher: /v1/* β†’ api-v1-service end Backend->>Client: Response Note over UM: Evaluation Order:
1. Host Rules
2. Route Rules (by priority)
3. Path Rules
4. Default Service

⚠️ Best Practices & Common Pitfalls

βœ… Best Practices

⚠️ Common Pitfalls

πŸ” Monitoring & Troubleshooting

URL Map Debugging

# Check URL map configuration gcloud compute url-maps describe problematic-url-map --global # Test specific routing scenarios gcloud compute url-maps test-route problematic-url-map \ --host=api.example.com \ --path="/api/v1/test" \ --headers="Content-Type=application/json,X-User-Type=premium" \ --global # Validate URL map structure gcloud compute url-maps validate problematic-url-map --global # Check target proxy configuration gcloud compute target-http-proxies describe my-target-proxy --global # Verify forwarding rule setup gcloud compute forwarding-rules describe my-forwarding-rule --global