Updated on 2024-09-02 GMT+08:00

Configuring URL Rewriting for a LoadBalancer Ingress

Dedicated LoadBalancer ingresses allow you to rewrite the URLs that match a regular expression. To rewrite the URL for such an ingress, do as follows:

  • Configure a path that matches a regular expression for the ingress, for example, /first/(.*)/(.*)/end.
  • Configure a rewrite annotation to match the regular expression in the path.

For example:

  • Set path to /first/(.*)/(.*)/end and annotation to /$1/$2. When the request sent by the user is /first/aaa/bbb/end, the path matches /first/(.*)/(.*)/end. The rewriting rule replaces $1 with aaa and $2 with bbb, the request path received by the backend server is /aaa/bbb.
  • Set path to /first/(.*)/end and annotation to /newpath/$1. When the request sent by the user is /first/aaa/end, the path matches /first/(.*)/end. The rewriting rule replaces $1 with aaa, the request path received by the backend server is /newpath/aaa.

This function relies on the ELB capability and is being introduced to all regions. Before using this function, verify if it is available by checking the console options in the current region.

Prerequisites

Notes and Constraints

  • The advanced forwarding policy of LoadBalancer ingresses is available only to dedicated load balancers.
  • The advanced forwarding policy of LoadBalancer ingresses is available only in clusters of version v1.23.14-r0, v1.25.9-r0, v1.27.6-r0, v1.28.4-r0, or later.

Configuring URL Rewriting for a LoadBalancer Ingress

You can configure a URL rewriting rule for an ingress using either the CCE console or kubectl.

  1. Log in to the CCE console and click the cluster name to access the cluster console.
  2. Choose Services & Ingresses in the navigation pane, click the Ingresses tab, and click Create Ingress in the upper right corner.
  3. Configure ingress parameters.

    This example explains only key parameters for configuring rewrite. You can configure other parameters as required. For details, see Creating a LoadBalancer Ingress on the Console.

    Table 1 Key parameters

    Parameter

    Description

    Example

    Name

    Enter an ingress name.

    ingress-test

    Load Balancer

    Select a load balancer to be associated with the ingress or automatically create a load balancer. In this example, only dedicated load balancers are supported.

    Dedicated

    Listener

    • External Protocol: HTTP and HTTPS are available.
    • External Port: specifies the port of the load balancer listener.
    • External Protocol: HTTP
    • External Port: 80

    Forwarding Policy

    • Domain Name: Enter actual domain name to be accessed. If it is left blank, the ingress can be accessed through the IP address. Ensure that the domain name has been registered and licensed. Once a forwarding policy is configured with a domain name specified, you must use the domain name for access.
    • Path Matching Rule: Select RegEx match if URL rewriting is used.
    • Path: Enter the path provided by a backend application for external access. The path added must be valid in the backend application, or the forwarding cannot take effect.
    • Destination Service: Select an existing Service or create a Service. Any Services that do not match the search criteria will be filtered out automatically.
    • Destination Service Port: Select the access port of the destination Service.
    • Actions
      • Operation: Select Rewrite URL. When an access request meets the forwarding policy, the URL will be rewritten based on the matching rule. You are allowed to configure a regular expression for the patch matching rule, and the result obtained using the regular expression can be used for rewriting the URL.
      • Path: A proper rule matching a regular expression must start with a slash (/).
    • Domain Name: example.com
    • Path Matching Rule: RegEx match
    • Path: /first/(.*)/(.*)/end
    • Destination Service: nginx
    • Destination Service Port: 80
    • Actions:
      • Operation: Rewrite URL
      • Path: /$1/$2
    Figure 1 Configuring a URL rewriting rule for an ingress

  4. Click OK.

URL rewriting rules of an ingress can be configured using annotations. The following is an example:

  1. Use kubectl to access the cluster. For details, see Connecting to a Cluster Using kubectl.
  2. Create a YAML file named ingress-test.yaml. The file name can be customized.

    vi ingress-test.yaml

    An example YAML file of an ingress associated with an existing load balancer is as follows:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: test-rewrite-url
      namespace: default
      annotations:
        kubernetes.io/elb.id: df76342f-e898-402a-bac8-bde5bf974da8
        kubernetes.io/elb.class: performance
        kubernetes.io/elb.port: '80'
        kubernetes.io/elb.rewrite-target: /$1/$2       # Rewrite path
    spec:
      rules:
        - host: "example.com"
          http:
            paths:
              - path: /first/(.*)/(.*)/end      # This path will be rewritten.
                backend:
                  service:
                    name: test-service
                    port:
                      number: 80
                property:
                  ingress.beta.kubernetes.io/url-match-mode: REGEX
                pathType: ImplementationSpecific
      ingressClassName: cce
    Table 2 Key parameters

    Parameter

    Mandatory

    Type

    Description

    kubernetes.io/elb.rewrite-target

    Yes

    String

    Information about the rewritten path.

    Format: A proper rule matching a regular expression must start with a slash (/).

    Parameter: This configuration takes effect on the URL of a single ingress matching the regular expression. After the configuration is deleted, the target URL rewriting rule will be automatically cleared.

    Either this annotation or the annotation of a grayscale release can be configured.

  3. Create an ingress.

    kubectl create -f ingress-test.yaml

    If information similar to the following is displayed, the ingress has been created:

    ingress/test-rewrite-url created

  4. Check the created ingress.

    kubectl get ingress

    If information similar to the following is displayed, the ingress has been created:

    NAME              CLASS    HOSTS     ADDRESS          PORTS   AGE
    test-rewrite-url  cce      *         121.**.**.**     80      10s

  5. Use curl to verify the rewriting, where ${ELB_IP} is the IP address accessed by the target ingress.

    # curl -H "Host:example.com" ${ELB_IP}/first/aaa/bbb/end

    The access path will be rewritten to /aaa/bbb.