Domain / host based routing not path based routing

Trying to configure domain based routing so that there are multiple APIs at path / proxied by the same Tyk gateway.

Is this supported? I sure hope the answer is yes.

All searches lead to old documentation which talks about Host Manager and NGINX.

Ideally we don’t have to run NGINX in front of Tyk to inject paths that Tyk uses for routing and then strips before proxying to target API servers.

There is a domain property listed in API Definition, however even though I’ve set it differently for two different API configurations with / path, the log indicates:

time=“Nov 18 22:13:03” level=info msg=“–> Loading API: test”
time=“Nov 18 22:13:03” level=info msg=“----> Tracking: tyk-test.com
time=“Nov 18 22:13:03” level=info msg=“----> Checking security policy: JWT”
time=“Nov 18 22:13:03” level=info msg=“----> Setting Listen Path: /”
time=“Nov 18 22:13:03” level=info msg=“–> Loading API: www”
time=“Nov 18 22:13:03” level=error msg=“Duplicate listen path found, skipping. API ID: 1”
time=“Nov 18 22:13:03” level=warning msg=“----> Skipped!”

The domain for www was tyk-www.com.

1 Like

Hi, can you share your tyk.conf file?

{
“listen_port”: 8080,
“secret”: “252d20ee67be67f6340b4c0605b044b6”,
“template_path”: “/opt/tyk-gateway/templates”,
“tyk_js_path”: “/opt/tyk-gateway/js/tyk.js”,
“middleware_path”: “/opt/tyk-gateway/middleware”,
“use_db_app_configs”: false,
“app_path”: “/opt/tyk-gateway/apps/”,
“storage”: {
“type”: “redis”,
“host”: “tyk_redis”,
“port”: 6379,
“username”: “”,
“password”: “”,
“database”: 0,
“optimisation_max_idle”: 100
},
“enable_analytics”: false,
“analytics_config”: {
“type”: “csv”,
“csv_dir”: “/tmp”,
“mongo_url”: “”,
“mongo_db_name”: “”,
“mongo_collection”: “”,
“purge_delay”: -1,
“ignored_ips”:
},
“health_check”: {
“enable_health_checks”: true,
“health_check_value_timeouts”: 60
},
“optimisations_use_async_session_write”: true,
“enable_non_transactional_rate_limiter”: true,
“enable_sentinel_rate_limiter”: false,
“allow_master_keys”: false,
“policies”: {
“policy_source”: “file”,
“policy_record_name”: “/opt/tyk-gateway/policies/policies.json”
},
“hash_keys”: true,
“close_connections”: true
}

Can you also share your two api definitions?

Out of the box, you can have multiple apis on the same listen path provided the domains that are assigned in the API definition are different between the two, and domain management is enabled in the gateway (which by default it is - but it looks like it’s missing from your Tyk.conf).

Enabling domains is done with:

"enable_custom_domains": true

In the Tyk.conf file.

1 Like

Success!!! Thanks, that fixed it.

Great, you’re welcome!

One concern/question. When the browser URL includes a host like www.foo.com and the Tyk API domain is set to foo.com, it returns 404. It seems the API domain needs to include the host too.

Is that expected? If the answer is yes, then technically it isn’t a domain, but a hostname and domain.

It took me quite a bit of hacking before figuring it out as I only put the domain in the Tyk API config and included the host www in the browser URL. The API Object Domain definition documentation could ideally have a bit more explanation and include the fact that the tyk.conf enable_custom_domains needs to be set to true. If those had been there it would have taken me minutes instead of hours to get domain (really hostname) routing working.

Hi Troy,

It’s the hostname that is required, and it’s an explicit match, subdomain flattening such as www -> root needs to be handled elsewhere - apologies for the confusion.

M