Skip to content

Question: How to proxy same UpstreamPathTemplate to different service base on remote ip? #1725

Closed

Description

@NullRefer

Hello, I'm using Ocelot as API gateway. And I'm encountering a situation.

As for same UpstreamPathTemplate, I need to proxy it to difference backend service base on request ip address.

Given current configuration

{
    "DownstreamPathTemplate": "/api{catchall}",
    "DownstreamHostAndPorts": [
            {
                "Host": "my-order-webapi-service",
                "Port": 8080,
            }
        ],
    "UpstreamPathTemplate": "/order{catchall}",
    "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ]
}

If ip1, ip2 request for GET /order/1, it goes to my-order1-webapi-service;
If ip3, ip4 request for GET /order/1, it goes to my-order2-webapi-service;
And the other requests go to my-order-webapi-service.

I ask for ChatGPT, it give me an configuration of below. But ocelot starts with exception that /order{catchall} has duplicate.

How could I change my configuration to solve it?

{
    "DownstreamPathTemplate": "/api{catchall}",
    "DownstreamHostAndPorts": [
            {
                "Host": "my-order-webapi-service",
                "Port": 8080,
            }
        ],
    "UpstreamPathTemplate": "/order{catchall}",
    "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ]
}
{
    // for order1
    "DownstreamPathTemplate": "/api{catchall}",
    "DownstreamHostAndPorts": [
            {
                "Host": "my-order1-webapi-service",
                "Port": 8080,
            }
        ],
    "UpstreamPathTemplate": "/order{catchall}",
    "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ]
    "RouteClaimsRequirement": {
        "sourceIp": "ip1|ip2"
    }
},
{
    // for order2
    "DownstreamPathTemplate": "/api{catchall}",
    "DownstreamHostAndPorts": [
            {
                "Host": "my-order2-webapi-service",
                "Port": 8080,
            }
        ],
    "UpstreamPathTemplate": "/order{catchall}",
    "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ]
    "RouteClaimsRequirement": {
        "sourceIp": "ip3|ip4"
    }
}

Specifications

  • Version: v17.0.0
  • Platform: Linux
  • Subsystem: .net5.0

Activity

  1. locked and limited conversation to collaborators on Oct 11, 2023
  2. converted this issue into a discussion #1726 on Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions