Dynamically set virtual hosts

83 views
Skip to first unread message

Oleg Yurochkin

unread,
Oct 11, 2023, 3:21:09 AM10/11/23
to KrakenD Community
Hello, we have multiple environments with different host names and I want to set them dynamycally with environment variables or somehow else.
There is example, where I want to set with env variable MY_HOST_VARIABLE  Is it possible?
{
          "endpoint": "/__virtual/{{env MY_HOST_VARIABLE}}/endpoint/*",
          "method": "POST",
          "input_headers": ["*"],
          "backend": [
            {
              "url_pattern": "/endpoint/",
              "method": "POST",
              "host": [
                "http://my-app:8080"
              ],
              "extra_config": {
                "backend/http": {
                  "return_error_code": true
                }
              }
            }
          ]
        }

Albert Lombarte

unread,
Oct 11, 2023, 3:27:06 AM10/11/23
to KrakenD Community, Oleg
Hello Oleg,

This is possible and works when enabling flexible configuration, but the sample configuration you have passed uses the Virtual Hosts functionality that is only available on the Enterprise Edition, and this won't have any effect on the community edition.

El dia dimecres, 11 d’octubre de 2023 a les 9:21:09 UTC+2, Oleg va escriure:

Oleg

unread,
Oct 11, 2023, 3:44:52 AM10/11/23
to KrakenD Community, Albert Lombarte, Oleg
Hello Albert,

Correct, I'm using enterprise edition with license and set FC_ENABLE : 1. If I use normal domain:
 {
          "endpoint": "/__virtual/my.domain.com/endpoint/*",
          "method": "POST",
          "input_headers": ["*"],
          "backend": [
            {
              "url_pattern": "/endpoint/",
              "method": "POST",
              "host": [
                "http://my-app:8080"
              ],
              "extra_config": {
                "backend/http": {
                  "return_error_code": true
                }
              }
            }
          ]
        }
It works fine. But when I set environment variable MY_HOST_VARIABLE = my.domain.com and try to reach my.domain.com/endpoint/123 krakend respond with 404 status code

Albert Lombarte

unread,
Oct 11, 2023, 7:37:00 AM10/11/23
to KrakenD Community, Oleg, Albert Lombarte
Thanks Oleg,

I am passing this conversation to KrakenD support then. Will get in touch shortly.

El dia dimecres, 11 d’octubre de 2023 a les 9:44:52 UTC+2, Oleg va escriure:

Oleg

unread,
Oct 11, 2023, 7:38:03 AM10/11/23
to KrakenD Community, Oleg, Albert Lombarte
Found that it works if you set env variable  MY_HOST_VARIABLE  and config looks as:
{
          "endpoint": "/__virtual/{MY_HOST_VARIABLE}/endpoint/*",
          "method": "POST",
          "input_headers": ["*"],
          "backend": [
            {
              "url_pattern": "/endpoint/",
              "method": "POST",
              "host": [
                "http://my-app:8080"
              ],
              "extra_config": {
                "backend/http": {
                  "return_error_code": true
                }
              }
            }
          ]
        }

But krakend failing (conflict error) if you set 2 different environment host variables ( MY_HOST_VARIABLE and MY_HOST_VARIABLE2)
{
          "endpoint": "/__virtual/{MY_HOST_VARIABLE}/endpoint/*",
          "method": "POST",
          "input_headers": ["*"],
          "backend": [
            {
              "url_pattern": "/endpoint/",
              "method": "POST",
              "host": [
                "http://my-app:8080"
              ],
              "extra_config": {
                "backend/http": {
                  "return_error_code": true
                }
              }
            }
          ]
        },
{
          "endpoint": "/__virtual/{MY_HOST_VARIABLE2}/endpoint/*",
          "method": "POST",
          "input_headers": ["*"],
          "backend": [
            {
              "url_pattern": "/endpoint/",
              "method": "POST",
              "host": [
                "http://my-app2:8080"
Reply all
Reply to author
Forward
0 new messages