Creating an HTTP router for HTTP traffic
To create an HTTP router and add a route to it:
-
In the management console
, select the folder where you want to create an HTTP router. -
In the list of services, select Application Load Balancer.
-
In the left-hand panel, select
HTTP routers. -
Click Create HTTP router.
-
Enter the HTTP router name.
-
Under Virtual hosts, click Add virtual host.
-
Enter the host name.
-
(Optional) In the Security profile field, select the Yandex Smart Web Security security profile.
-
Click Add route.
-
Enter the route Name.
-
In the Path field, select one of the options:
Matches
and specify the path/
to route all requests that match the specified path.Starts with
to route all requests with a specific prefix.Regular expression
to route all requests that match a RE2 regular expression .
-
In the HTTP methods list, select the required methods.
-
In the Action field, select one of the options:
Routing
,Forward
, orResponse
. Depending on the selected option:Routing
:- In the Backend group field, select the name of the backend group from the same folder where you are creating the HTTP router.
- (Optional) In the Rewrite path or start field, specify where the HTTP router should redirect traffic. If you select
Matches
in the Path field, the path will be completely rewritten. If you selectStarts with
, only the prefix will be rewritten. - (Optional) In the Host header rewrite field, select one of the options:
none
: Do not rewrite.rewrite
: Rewrite the header to the specified value.auto
: Automatically rewrite the header to the target VM address.
- (Optional) In the Timeout, s field, specify the maximum connection time.
- (Optional) In the Idle timeout, seconds field, specify the maximum connection keep-alive time with zero data transmission.
- (Optional) In the Valid values for the Upgrade header field, list the protocols the backend group can switch to within a TCP connection upon the client's request.
- (Optional) Select WebSocket if you want to use the WebSocket protocol.
Forward
:- In the HTTP status code field, select the code to be used for forwarding.
- (Optional) In the Rewrite path or start field, specify where the HTTP router should redirect traffic. If you select
Matches
in the Path field, the path will be completely rewritten. If you selectStarts with
, only the prefix will be rewritten. - (Optional) Select the Delete query parameters option.
- (Optional) Select the Replace scheme option.
- (Optional) Select Replace host and specify a new host.
- (Optional) Select Replace port and specify a new port.
Response
:- In the HTTP status code field, select the code to be used for response.
- In the Response body field, click Select and do the following in the window that opens:
- Select a response Method: Text or File.
- Depending on the selected method, attach a file or specify the response text.
-
Click Create.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
See the description of the CLI command to create an HTTP router:
yc alb http-router create --help
-
Run this command:
yc alb http-router create <HTTP_router_name>
Result:
id: a5dcsselagj4******** name: test-http-router folder_id: aoerb349v3h4******** created_at: "2021-02-11T21:04:59.438292069Z"
-
View a description of the CLI command for creating a virtual host:
yc alb virtual-host create --help
-
Create a virtual host, specifying the name of the HTTP router and the virtual host settings:
yc alb virtual-host create <virtual_host_name> \ --http-router-name <HTTP_router_name> \ --authority your-domain.foo.com \ --modify-request-header name=Accept-Language,append=ru-RU \ --security-profile-id <security_profile_ID>
Where:
--http-router-name
: HTTP router name.--authority
: Domains for theHost
andauthority
headers that will be associated with this virtual host. Wildcards are supported, for example,*.foo.com
or*-bar.foo.com
.--modify-request-header
: Settings for modifying request headers:name
: Name of the header to be modified.append
: String to be added to the header value.
--security-profile-id
: (Optional) ID of the Yandex Smart Web Security security profile.
Result:
name: test-virtual-host authority: - your-domain.foo.com modify_request_headers: - name: Accept-Language append: ru-RU
-
View a description of the CLI command for adding a host:
yc alb virtual-host append-http-route --help
-
Add a route, specifying the HTTP router ID or name and the routing parameters:
yc alb virtual-host append-http-route <route_name> \ --virtual-host-name <virtual_host_name> \ --http-router-name <HTTP_router_name> \ --prefix-path-match / \ --backend-group-name <backend group name> \ --request-timeout <request_timeout>s \ --request-idle-timeout <request_idle_timeout>s
Where:
-
--virtual-host-name
: Virtual host name. -
--http-router-name
: HTTP router name. -
--prefix-path-match
: Parameter for routing all requests with a given prefix. The parameter should be followed with/
.To specify a routing condition, you can also use the following parameters:
--exact-path-match
to route all requests that match the specified path. The parameter should be followed with/<path>/
.--regex-path-match
to route all requests that match a RE2 regular expression . The parameter should be followed with/<regular_expression>
.
-
--backend-group-name
: Backend group name. -
--request-timeout
: Request timeout, seconds. -
--request-max-timeout
: Maximum request idle timeout in seconds.
For more information about the
yc alb virtual-host append-http-route
command parameters, see the CLI reference.Result:
done (1s) name: test-virtual-host authority: - your-domain.foo.com routes: - name: test-route http: match: path: prefix_match: / route: backend_group_id: a5d4db973944******** timeout: 2s idle_timeout: 3s modify_request_headers: - name: Accept-Language append: ru-RU
-
Terraform
For more information about the provider resources, see the documentation on the Terraform
If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
If you don't have Terraform, install it and configure the Yandex Cloud provider.
-
In the configuration file, specify the parameters of the HTTP router and virtual host:
resource "yandex_alb_http_router" "tf-router" { name = "<HTTP_router_name>" labels = { tf-label = "tf-label-value" empty-label = "" } } resource "yandex_alb_virtual_host" "my-virtual-host" { name = "<virtual_host_name>" http_router_id = yandex_alb_http_router.tf-router.id route { name = "<route_name>" http_route { http_route_action { backend_group_id = "<backend_group_ID>" timeout = "60s" } } } route_options { security_profile_id = "<security_profile_ID>" } }
Where:
yandex_alb_http_router
: HTTP router description:-
name
: HTTP router name. The name format is as follows:- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
labels
: HTTP router labels. Set a key-value pair.
-
yandex_alb_virtual_host
: Virtual host description:-
name
: Virtual host name. The name format is as follows:- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
http_router_id
: HTTP router ID. -
route
: HTTP router route description:name
: Route name.http_route_action
: Parameter to indicate an action on HTTP traffic.backend_group_id
: Backend group ID.timeout
: Maximum request idle timeout in seconds.
-
route_options
: (Optional) Additional parameters of the virtual host:security_profile_id
: Yandex Smart Web Security security profile ID.
-
For more information about the parameters of resources used in Terraform, see the provider documentation:
-
Create resources:
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
Terraform will create all the required resources. You can check the new resources and their configuration using the management console
or this CLI command:yc alb http-router get <HTTP_router_name>
-
Use the create REST API method for the HttpRouter resource or the HttpRouterService/Create gRPC API call.