---
title: "Load balance traffic by request URL"
sidebar_label: "Load balance traffic by request URL"
sidebar_position: 2
description: "How to set up a traffic balancing rule based on the domain or request path"
---

import Formbricks from '@theme/MDXComponents/Formbricks'

import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import {TabItemLabel} from '@selectel/docux/components'
import MoreVerticalIcon from '@selectel/docux/icons/more-vertical'
import CopyIcon from '@selectel/docux/icons/copy'

# Load balance traffic by request URL

You can direct traffic to different [target groups](/cloud-servers/load-balancers/about-load-balancers.mdx#target-groups) of servers depending on the domain and path specified in the request.

<Tabs queryString="redirect-by-url">
  <TabItem value="panel" default>
    <TabItemLabel>
      Control panel
    </TabItemLabel>

    1. In the [control panel](https://my.selectel.ru/vpc/default/lbaas/load-balancers/), on the top menu, click **Products** and select **Cloud Servers**.

    2. Go to the **Load Balancers** section → **Load Balancers** tab.

    3. Open the load balancer page.

    4. Click **Create Rule**.

    5. Select the traffic reception protocol — HTTPS. The standard port 443 will be automatically selected, on which the load balancer will listen for traffic.

    6. Select a certificate to terminate HTTPS traffic on the load balancer — choose a certificate from the Secrets Manager or upload a new one. If the rule is to receive traffic for multiple domains, the certificate must be valid for all domains, or [add multiple certificates](/cloud-servers/load-balancers/manage/ssl-certificates.mdx#add-certificates).

    7. Select a default target group — traffic that does not fall under any [HTTP policies](/cloud-servers/load-balancers/about-load-balancers.mdx#http-policies) will be directed there. Select a group from the list or [create a new target group](/cloud-servers/load-balancers/target-groups/create-target-group.mdx), specify the HTTP traffic destination protocol and port 80 for it. If you want to drop traffic that does not fall under an HTTP policy, select **Without target group**.

    8. Select the [HTTP request headers](/cloud-servers/load-balancers/about-load-balancers.mdx#http-request-headers) that will be passed to the servers.

    9. Create an [HTTP policy](/cloud-servers/load-balancers/about-load-balancers.mdx#http-policies) to use for traffic balancing. To do this, click **Add new policy**.

    10. To balance a request by domain name, select the check condition: **HOSTNAME** — **EQUAL TO**.

    11. Enter the domain name without the protocol, for example `example.com`.

    12. Click **New condition**.

    13. To balance a request by path, select the check condition: **PATH** — **STARTS WITH**.

    14. Enter the text that the request path should start with, for example `/api`.

    15. Select the **Redirect to target group** action.

    16. Select the target group where traffic that matches the policy will be directed. To create a new target group, click **New target group** and [create a target group](/cloud-servers/load-balancers/target-groups/create-target-group.mdx) with the HTTP protocol and port 80. If you want to drop traffic that does not fall under an HTTP policy, select **Without target group**.

    17. Optional: change the policy name or leave the one generated by default.

    18. Click **Add**.

    19. Optional: open the **Advanced rule settings** block and specify the [connection settings](/cloud-servers/load-balancers/about-load-balancers.mdx#connection-settings):

        * for incoming requests to the load balancer — specify the connection timeout and maximum number of connections;
        * for requests from the load balancer to servers — specify the connection timeout, inactivity timeout, and TCP packet wait timeout.

    20. Click **Create**.
  </TabItem>

  <TabItem value="openstack">
    <TabItemLabel>
      OpenStack CLI
    </TabItemLabel>

    1. [Open the OpenStack CLI](/cloud-servers/tools/openstack-cli/).

    2. If you do not have a [target group](/cloud-servers/load-balancers/about-load-balancers.mdx#target-groups) to balance traffic into yet, create one:

       ```bash
       openstack loadbalancer pool create \
         --name <pool_name> \
         --lb-algorithm <algorithm> \
         --loadbalancer <loadbalancer> \
         --protocol HTTP
       ```

       Specify:

       * `<pool_name>` — name of the target group;
       * `<algorithm>` — name of the algorithm: `ROUND_ROBIN` or `LEAST_CONNECTIONS`
       * `<loadbalancer>` — ID or name of the load balancer. You can view the list with the command `openstack loadbalancer list`.

    3. If you created a target group in step 2, add a server to it:

       ```bash
       openstack loadbalancer member create \
         --subnet-id <subnet_uuid> \
         --address <server_ip_address> \
         --protocol-port 80 \
         <pool>
       ```

       Specify:

       * `<subnet_uuid>` — ID of the server's private or public subnet. You can view the list with the command `openstack subnet list`;
       * `<server_ip_address>` — private IP address of the server. You can copy it in the [control panel](https://my.selectel.ru/vpc/default/servers/): from the top menu, click **Products** → **Cloud Servers** → server page → tab **Ports** → in the port card, click <CopyIcon /> next to the IP address;
       * `<pool>` — ID or name of the target group created in step 2.

    4. Create a [listener](/cloud-servers/load-balancers/about-load-balancers.mdx#rules) for the load balancer with the TERMINATED\_HTTPS protocol and port 443:

       ```bash
       openstack loadbalancer listener create \
         --name <listener_name> \
         --protocol TERMINATED_HTTPS \
         --protocol-port 443 \
         --default-tls-container=<certificate_uuid> \
         --default-pool <default_pool> \
         <loadbalancer>
       ```

       Specify:

       * `<listener_name>` — listener name;
       * `<certificate_uuid>` — ID of the TLS(SSL) certificate for traffic termination on the load balancer. You can copy it in the [control panel](https://my.selectel.ru/certificates/): from the top menu, click **Products** → **Certificate Manager** → in the <MoreVerticalIcon /> certificate menu, select **Copy UUID**. If the listener will accept traffic for multiple domains, the certificate must be valid for all domains, or [add multiple certificates](/cloud-servers/load-balancers/manage/ssl-certificates.mdx#add-certificates);
       * optional: `--default-pool <default_pool>` — ID or name of the default target group where traffic not covered by [HTTP policies](/cloud-servers/load-balancers/about-load-balancers.mdx#http-policies) in the listener will be sent. The list of groups can be viewed using the `openstack loadbalancer pool list` command. If you do not have a suitable target group, [create one](/cloud-servers/load-balancers/target-groups/create-target-group.mdx). If this parameter is not specified, traffic not covered by policies will be dropped;
       * `<loadbalancer>` — ID or name of the load balancer. The list can be viewed using the `openstack loadbalancer list`.

    5. Create an [HTTP policy](/cloud-servers/load-balancers/about-load-balancers.mdx#http-policies) in the listener:

       ```bash
       openstack loadbalancer l7policy create \
         --action REDIRECT_TO_POOL \
         --redirect-pool <pool> \
         --name <policy_name> \
         <listener_name>
       ```

       Specify:

       * `<pool>` — ID or name of the target group. The list can be viewed using the `openstack loadbalancer pool list`;
       * `<policy_name>` — name of the L7 policy;
       * `<listener_name>` — name of the listener you set in step 4.

    6. To balance a request by domain name, create a condition in the HTTP policy to check the domain:

       ```bash
       openstack loadbalancer l7rule create \
         --compare-type EQUAL_TO \
         --type HOST_NAME \
         --value <domain_name> \
         <policy>
       ```

       Specify:

       * `<domain_name>` — a domain like `example.com`;
       * `<policy>` — ID or name of the L7 policy. The list can be viewed using the `openstack loadbalancer l7policy list`.

    7. To balance a request by path, create a condition in the HTTP policy to check the path:

       ```bash
       openstack loadbalancer l7rule create \
         --compare-type STARTS_WITH \
         --type PATH \
         --value <path> \
         <policy>
       ```

       Specify:

       * `<path>` — text the request path must start with, for example `/api`;
       * `<policy>` — ID or name of the L7 policy. The list can be viewed using the `openstack loadbalancer l7policy list`.
  </TabItem>
</Tabs>

<Formbricks />
