Using URL maps

This guide shows you how to configure Google Cloud URL maps. Before following this guide, familiarize yourself with URL map concepts.

URL maps are used with the following Google Cloud products:

Internal HTTP(S) Load Balancing and Traffic Director support advanced traffic management use cases. For more information, see:

Configuring URL maps

You can add backend services and backend buckets to a URL map.

Backend buckets are supported with external HTTP(S) load balancers only.

Adding a URL map

Console

To add a URL map using the Cloud Console, perform the following steps:

  1. Go to the Load balancing page.

    Go to the Load balancing page

  2. Click the Name of a load balancer.
  3. On the Load Balancer Details screen, click Edit for the selected load balancer.
  4. Select Host and path rules.
  5. Click Add host and path rule.
  6. Fill in the Host field, Paths field, or both, and select a backend service or backend bucket.
  7. Look for the blue checkmark to the left of Host and Path Rules and click the Update button.

gcloud

To add a URL map using the gcloud command-line tool, use the url-maps create command:

gcloud compute url-maps create URL_MAP_NAME \
   (--default-backend-bucket=DEFAULT_BACKEND_BUCKET | --default-service=DEFAULT_SERVICE) \
   [--description DESCRIPTION] \
   [--global | --region=REGION]

For internal HTTP(S) load balancers, make sure to include the --region flag when you create the URL map.

A newly created URL map matches only one path, the default one (/*). This default path matcher is created automatically. All traffic that does not match a manually created path matcher or host rule is sent to the URL map default backend service.

Adding a path matcher

A path matcher maps HTTP(S) request paths or path prefixes (for example, /video) to backend services. All traffic that matches the specified path is processed by path rules in the path matcher. If a path matches a path rule, then the request is forwarded to the backends associated with that path rule. If a request matches the path matcher no path rules, then the request is forwarded to the default backend for the path matcher.

Console

To create a path matcher using the Cloud Console, perform the following steps:

  1. If you're not already on the Host and path rules screen, go to the Load balancing page.

    Go to the Load balancing page

  2. Click the Name of a load balancer.
  3. On the Load Balancer Details screen, click Edit for the selected load balancer.
  4. In the Host and path rules screen, either click Add host and path rule or click an existing Paths field.
  5. Enter the path to configure, for example, /video.
  6. Look for the blue checkmark to the left of Host and Path Rules and click the Update button.

gcloud

To create a path matcher using the gcloud command-line tool, use the gcloud compute url-maps add-path-matcher command:

gcloud compute url-maps add-path-matcher URL_MAP_NAME \
   --default-service BACKEND_SERVICE \
   --path-matcher-name PATH_MATCHER \
   [--path-rules="PATH=SERVICE"]

This command requires a default backend service to which it can send unmatched requests. Optionally, you can use the --path-rules flag to define mappings between request paths and backend services. The following example routes the request paths /video/ and /video/* to the video-service backend service:

--path-rules="/video=video-service,/video/*=video-service"

Both the url-maps create and url-maps add-path-matcher commands have a --default-service parameter. In create, the default service is the one used if none of the path matchers match the incoming URL. In add-path-matcher, the default service is the one used if the path matches the path matcher, but none of the --path-rules match.

Adding a host rule

Console

To create a host rule using the Cloud Console, perform the following steps:

  1. If you're not already on the Host and path rules screen, go to the Load balancing page.

    Go to the Load balancing page

  2. Click the Name of a load balancer.
  3. On the Load Balancer Details screen, click Edit for the selected load balancer.
  4. On the Host and path rules screen, either click Add host and path rule or click an existing Hosts field.
  5. Enter a fully-qualified Host name, for example web.example.com.
  6. Look for the blue checkmark to the left of Host and Path Rules and click the Update button.

gcloud

To create a host rule using the gcloud command-line tool, use the gcloud compute url-maps add-host-rule command:

gcloud compute url-maps add-host-rule URL_MAP_NAME \
    --hosts=HOSTS --path-matcher-name=PATH_MATCHER

The flag --hosts defines a set of hosts to match requests against. For example, the following --hosts value would match requests against www.example.com and any subdomain of google.com:

--hosts *.google.com,www.example.com

Selecting a backend service or backend bucket

Console

To select a backend service or backend bucket for a URL map, perform the following steps:

  1. If you're not already on the Host and path rules screen, go to the Load balancing page.

    Go to the Load balancing page

  2. Click the Name of a load balancer.
  3. On the Load Balancer Details screen, click Edit for the selected load balancer.
  4. In the Host and path rules screen, in the Backends field pull-down menu, select an available backend service or backend bucket.
  5. Look for the blue checkmark to the left of Host and Path Rules and click the Update button.

gcloud

To select a backend service or backend bucket using the gcloud command-line tool, use the url-maps set-default-service command.

gcloud compute url-maps set-default-service URL_MAP_NAME
  (--default-backend-bucket=DEFAULT_BACKEND_BUCKET
  | --default-service=DEFAULT_SERVICE)[GCLOUD_WIDE_FLAG ...]

Testing a URL map

You can add configuration tests to a URL map to ensure that your URL map routes requests to the backend services or backend buckets as intended.

When you edit your URL map, the tests run, and an error message appears if a test fails:

Error: Invalid value for field 'urlMap.tests': ''.
Test failure: Expect URL 'HOST/PATH' to map to service 'EXPECTED_BACKEND_SERVICE', but actually mapped to 'ACTUAL_BACKEND_SERVICE'.

Adding tests to URL maps is optional.

Console

To run tests from the Cloud Console:

  1. Go to the Load balancing page.

    Go to the Load balancing page

  2. Click the Name of a load balancer.
  3. On the Load Balancer Details screen, click Edit for the selected load balancer.
  4. Click Host and path rules.
  5. Beneath the Host and path rules, click Show configuration tests.
  6. Click Add configuration test. Add the following test URLs and backends:
    • Test URL example.com and Backend www-service.
    • Test URL example.net and Backend www-service.
    • Test URL example.net/web and Backend www-service.
    • Test URL example.com/videos and Backend video-service.
    • Test URL example.com/videos/browse and Backend video-service.
    • Test URL example.net/static and Backend static-service.
    • Test URL example.net/static/images and Backend static-service.
  7. Look for the blue checkmark to the left of Host and Path Rules and click the Update button.

gcloud

To add tests to your URL map using the gcloud command-line tool, use the gcloud compute url-maps edit command:

gcloud compute url-maps edit URL_MAP_NAME

This launches a text editor. For external HTTP(S) load balancers, your tests must use the following format:

  tests:
    - host: example.com
    service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/www-service
    - host: example.net
    service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/www-service
    - host: example.com
      path: /videos
      service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/video-service
    - host: example.com
      path: /videos/browse
      service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/video-service
    - host: example.net
      path: /web
      service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/www-service
    - host: example.net
      path: /static
      service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/static-service
    - host: example.net
      path: /static/images
      service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/static-service

For internal HTTP(S) load balancers, your service URLs must point to regional backend services. For example:

https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/backendServices//www-service

Note that if you do not specify a host in a host rule, then URLs from all hosts (both example.com and example.net) can match. If you do have host rules, then you must create rules that match both example.com and example.net.

Listing URL maps

Console

You cannot list all of your URL maps in the Cloud Console.

gcloud

To display a list of URL maps using the gcloud command-line tool, use the url-maps list command.

gcloud compute url-maps list

Getting information about a URL map

Console

To get information about a URL map, perform the following steps:

  1. Go to the Load balancing page.

    Go to the Load balancing page

  2. Click the Name of a load balancer.
  3. On the Load Balancer Details screen, click Edit for the selected load balancer.
  4. View the Host and path rules.

gcloud

To get information about a single URL map using the gcloud command-line tool, use the url-maps describe command.

gcloud compute url-maps describe URL_MAP_NAME

Deleting a URL map

You can delete a URL map only after you've deleted all target proxies that reference it. For more information, see Deleting a target proxy.

Console

To delete a URL map, perform the following steps:

  1. Go to the Load balancing page.

    Go to the Load balancing page

  2. Click the Name of a load balancer.
  3. On the Load Balancer Details screen, click Edit for the selected load balancer.
  4. On the Load Balancer Details screen, view the Host and path rules.
  5. Click the "X" to the right of a URL map to delete it. The URL map disappears.
  6. Look for the blue checkmark to the left of Host and Path Rules and click the Update button.

gcloud

To delete a URL map using the gcloud command-line tool, use the url-maps delete command. Before you can delete a URL map, any target HTTP proxies that reference the URL map must first be deleted.

gcloud compute url-maps delete URL_MAP_NAME [--quiet]

Deleting a path matcher

Console

To delete a path matcher, perform the following steps:

  1. Go to the Load balancing page.

    Go to the Load balancing page

  2. Click the Name of a load balancer.
  3. On the Load Balancer Details screen, click Edit for the selected load balancer.
  4. Select Host and path rules.
  5. In the Paths field for an existing URL map, click the "x" on the path matcher's name.
  6. Look for the blue checkmark to the left of Host and Path Rules and click the Update button.

gcloud

To delete a path matcher, use the gcloud compute url-maps remove-path-matcher command:

gcloud compute url-maps remove-path-matcher URL_MAP_NAME \
   [--path-matcher-name PATH_MATCHER]

Deleting a host rule

Console

To delete a host rule, perform the following steps:

  1. If you're not already on the Host and path rules screen, go to the Load balancing page.

    Go to the Load balancing page

  2. Click the Name of a load balancer.
  3. On the Load Balancer Details screen, click Edit for the selected load balancer.
  4. Select Host and path rules.
  5. In the Hosts field for an existing URL map, click the "x" on the host's name.
  6. Look for the blue checkmark to the left of Host and Path Rules and click the Update button.

gcloud

To delete a host rule from your URL map, use the gcloud compute url-maps remove-host-rule command:

gcloud compute url-maps remove-host-rule URL_MAP_NAME --host=HOST

For example, to remove a host rule that contains the host google.com from a URL map named my-map, you would run the following command:

gcloud compute url-maps remove-host-rule my-map --host google.com

What's next