Regex matching on listen_paths for an single API


#1

I have the requirement to do the following

Same domain, regex on a listen paths for one API

I’d like to be able to define ONE API like this: http://my-es-api/[^.*/_(search|count)$] -> backendA

Rather than multiple backends like:

API1 http://my-es-api/_search] -> backendA

API2 http://my-es-api/_count] -> backendA

Is this possible?


#2

Hello!

There is an option. If you have an API which is bind to “my-es-api” domain with “/” listen path, you can define “_search” and “_count” urls in API designer, and add URL rewrite filter. “Rewrite to” field support setting alternative upstream url so that pattern can look like http://<backendA>/_search

Leonid, Tyk Team


#3

Just to confirm, you are saying the “rewrite to” portion of that CAN take a fully qualified URI w/ http(s)://[host]?


#4

Ok so here is an export of an API def doing what you suggested (below)

I make a request to http://my-es-api.test.com:8080/192.168.0.148:8888/_count

I would expect that this will proxy to http://192.168.0.148:8888/_count (per the regex url rewrite below)

The issue is that it proxies to http://192.168.0.148:8888/_count/_count

I’m not seeing where the issue is?

{
    "id": "5978f060b46f2e00016bb4ac",
    "name": "ES API",
    "slug": "es-api",
    "api_id": "01ea2e80f1b741716e149ec8a5d6644d",
    "org_id": "5976507eb46f2e00016bb4a1",
    "use_keyless": true,
    "use_oauth2": false,
    "use_openid": false,
    "openid_options": {
        "providers": [],
        "segregate_by_client": false
    },
    "oauth_meta": {
        "allowed_access_types": [],
        "allowed_authorize_types": [],
        "auth_login_redirect": ""
    },
    "auth": {
        "use_param": false,
        "param_name": "",
        "use_cookie": false,
        "cookie_name": "",
        "auth_header_name": ""
    },
    "use_basic_auth": false,
    "enable_jwt": false,
    "use_standard_auth": false,
    "enable_coprocess_auth": false,
    "jwt_signing_method": "",
    "jwt_source": "",
    "jwt_identity_base_field": "",
    "jwt_client_base_field": "",
    "jwt_policy_field_name": "",
    "notifications": {
        "shared_secret": "",
        "oauth_on_keychange_url": ""
    },
    "enable_signature_checking": false,
    "hmac_allowed_clock_skew": -1,
    "base_identity_provided_by": "",
    "definition": {
        "location": "header",
        "key": "x-api-version"
    },
    "version_data": {
        "not_versioned": true,
        "versions": {
            "Default": {
                "name": "Default",
                "expires": "",
                "paths": {
                    "ignored": [],
                    "white_list": [],
                    "black_list": []
                },
                "use_extended_paths": true,
                "extended_paths": {
                    "url_rewrites": [
                        {
                            "path": "/*",
                            "method": "GET",
                            "match_pattern": "/(.*)/(_search|_count)",
                            "rewrite_to": "http://$1/$2"
                        }
                    ]
                },
                "global_headers": {},
                "global_headers_remove": [],
                "global_size_limit": 0,
                "override_target": ""
            }
        }
    },
    "uptime_tests": {
        "check_list": [],
        "config": {
            "expire_utime_after": 0,
            "service_discovery": {
                "use_discovery_service": false,
                "query_endpoint": "",
                "use_nested_query": false,
                "parent_data_path": "",
                "data_path": "",
                "port_data_path": "",
                "target_path": "",
                "use_target_list": false,
                "cache_timeout": 60,
                "endpoint_returns_list": false
            },
            "recheck_wait": 0
        }
    },
    "proxy": {
        "preserve_host_header": false,
        "listen_path": "/",
        "target_url": "http://192.168.0.148:9080/service/dontcare",
        "strip_listen_path": true,
        "enable_load_balancing": false,
        "target_list": [],
        "check_host_against_uptime_tests": false,
        "service_discovery": {
            "use_discovery_service": false,
            "query_endpoint": "",
            "use_nested_query": false,
            "parent_data_path": "",
            "data_path": "hostname",
            "port_data_path": "port",
            "target_path": "/api-slug",
            "use_target_list": false,
            "cache_timeout": 60,
            "endpoint_returns_list": false
        }
    },
    "disable_rate_limit": false,
    "disable_quota": false,
    "custom_middleware": {
        "pre": [],
        "post": [],
        "post_key_auth": [],
        "auth_check": {
            "name": "",
            "path": "",
            "require_session": false
        },
        "response": [],
        "driver": "",
        "id_extractor": {
            "extract_from": "",
            "extract_with": "",
            "extractor_config": {}
        }
    },
    "custom_middleware_bundle": "",
    "cache_options": {
        "cache_timeout": 60,
        "enable_cache": true,
        "cache_all_safe_requests": false,
        "cache_response_codes": [],
        "enable_upstream_cache_control": false
    },
    "session_lifetime": 0,
    "active": true,
    "auth_provider": {
        "name": "",
        "storage_engine": "",
        "meta": {}
    },
    "session_provider": {
        "name": "",
        "storage_engine": "",
        "meta": null
    },
    "event_handlers": {
        "events": {}
    },
    "enable_batch_request_support": false,
    "enable_ip_whitelisting": false,
    "allowed_ips": [],
    "dont_set_quota_on_create": false,
    "expire_analytics_after": 0,
    "response_processors": [],
    "CORS": {
        "enable": false,
        "allowed_origins": [],
        "allowed_methods": [],
        "allowed_headers": [],
        "exposed_headers": [],
        "allow_credentials": false,
        "max_age": 24,
        "options_passthrough": false,
        "debug": false
    },
    "domain": "my-es-api.test.com",
    "do_not_track": false,
    "tags": [],
    "enable_context_vars": false,
    "config_data": {}
}

#5

Are you running the latest version? There was a bug that caused this duplicate slug behaviour but AFAIK it was resolved?


#6

Yes latest of both gateway/dashboard 2.3.7 and 1.3.7