[GH-ISSUE #6355] [Feature]: Support chained load balancers for multi-stage traffic routing #4313

Open
opened 2026-10-31 20:38:45 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @Lemirda on GitHub (Aug 28, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6355

Currently, 3x-ui/Xray load balancing does not provide a convenient way to build a multi-stage load-balancing chain.

I have a setup with several Russian servers and several foreign servers. I would like traffic to be balanced in two stages:

First, select the least loaded Russian server from a group of Russian servers.

Send the user's traffic through the selected Russian server.

Then, on the selected Russian server, select the least loaded foreign server from a second group of servers.

Finally, send the traffic to the Internet through that foreign server.

For example:

Client
↓
Inbound
↓
RU Load Balancer
├── RU-1
├── RU-2
└── RU-3
↓
Foreign Load Balancer
├── DE-1
├── NL-1
└── FI-1
↓
Internet

The important requirement is that once a connection has been assigned to a Russian server, it should remain associated with that server for the lifetime of the connection. The Russian server should then perform the second-stage balancing.

Describe the solution you'd like

Please add support for chained/nested load balancers in 3x-ui.

For example, allow one balancer to use another balancer as its outbound/next hop:

ru-balancer
↓
RU nodes
↓
foreign-balancer
↓
foreign nodes

Ideally this could be configured directly in the 3x-ui interface.

For example:

First balancer
Name: ru-balancer
Strategy: leastLoad

Nodes:

  • RU-1
  • RU-2
  • RU-3
    Second balancer
    Name: foreign-balancer
    Strategy: leastLoad

Nodes:

  • DE-1
  • NL-1
  • FI-1

The first balancer would select the Russian node, and the traffic would then be passed through that node to the second balancer.

It would also be useful if balancers could be selected as a valid next-hop/outbound for another balancer, instead of only selecting individual outbounds.

Alternatives you've considered

Using Xray JSON manually with custom outbound chaining/dialer configuration.

However, this is difficult to maintain in 3x-ui because the configuration is generated by the panel and the required multi-stage routing is not conveniently configurable through the UI.

Another option is to deploy a separate routing/load-balancing layer, but this adds unnecessary complexity and another component to maintain.

Concrete use case

I operate several Russian entry servers and several foreign exit servers.

I want users to connect to a single inbound, while 3x-ui automatically performs:

Inbound
→ least-loaded RU server
→ least-loaded foreign server
→ Internet

For example:

RU servers:
RU-1: 30% load
RU-2: 70% load
RU-3: 45% load

    ↓

Select RU-1

    ↓

Foreign servers:
DE-1: 60%
NL-1: 35%
FI-1: 50%

    ↓

Select NL-1

This would allow the Russian entry infrastructure and foreign exit infrastructure to be scaled independently.

Mockups, screenshots, or examples

The first balancer should support existing Xray balancing strategies such as leastLoad / leastPing.
The second balancer should work independently after the first node has been selected.
Existing connection affinity/stickiness should be preserved for established connections.
The feature should be configurable through the 3x-ui UI without manually editing the generated Xray configuration.
Existing single-level balancers should continue to work exactly as they do now.

Which parts of the panel would this affect?

Frontend (UI / panel pages), Xray config generation, Multi-node (sub-nodes), Other

Are you willing to help?

  • I'd like to implement this feature myself and open a PR.
  • I can help test once a PR is open.

Before submitting

  • I searched existing issues and this feature has not been requested.
  • This feature request is written in English.
Originally created by @Lemirda on GitHub (Aug 28, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6355 ### Is your feature request related to a problem? Currently, 3x-ui/Xray load balancing does not provide a convenient way to build a multi-stage load-balancing chain. I have a setup with several Russian servers and several foreign servers. I would like traffic to be balanced in two stages: First, select the least loaded Russian server from a group of Russian servers. Send the user's traffic through the selected Russian server. Then, on the selected Russian server, select the least loaded foreign server from a second group of servers. Finally, send the traffic to the Internet through that foreign server. For example: Client ↓ Inbound ↓ RU Load Balancer ├── RU-1 ├── RU-2 └── RU-3 ↓ Foreign Load Balancer ├── DE-1 ├── NL-1 └── FI-1 ↓ Internet The important requirement is that once a connection has been assigned to a Russian server, it should remain associated with that server for the lifetime of the connection. The Russian server should then perform the second-stage balancing. ### Describe the solution you'd like Please add support for chained/nested load balancers in 3x-ui. For example, allow one balancer to use another balancer as its outbound/next hop: ru-balancer ↓ RU nodes ↓ foreign-balancer ↓ foreign nodes Ideally this could be configured directly in the 3x-ui interface. For example: First balancer Name: ru-balancer Strategy: leastLoad Nodes: - RU-1 - RU-2 - RU-3 Second balancer Name: foreign-balancer Strategy: leastLoad Nodes: - DE-1 - NL-1 - FI-1 The first balancer would select the Russian node, and the traffic would then be passed through that node to the second balancer. It would also be useful if balancers could be selected as a valid next-hop/outbound for another balancer, instead of only selecting individual outbounds. ### Alternatives you've considered Using Xray JSON manually with custom outbound chaining/dialer configuration. However, this is difficult to maintain in 3x-ui because the configuration is generated by the panel and the required multi-stage routing is not conveniently configurable through the UI. Another option is to deploy a separate routing/load-balancing layer, but this adds unnecessary complexity and another component to maintain. ### Concrete use case I operate several Russian entry servers and several foreign exit servers. I want users to connect to a single inbound, while 3x-ui automatically performs: Inbound → least-loaded RU server → least-loaded foreign server → Internet For example: RU servers: RU-1: 30% load RU-2: 70% load RU-3: 45% load ↓ Select RU-1 ↓ Foreign servers: DE-1: 60% NL-1: 35% FI-1: 50% ↓ Select NL-1 This would allow the Russian entry infrastructure and foreign exit infrastructure to be scaled independently. ### Mockups, screenshots, or examples The first balancer should support existing Xray balancing strategies such as leastLoad / leastPing. The second balancer should work independently after the first node has been selected. Existing connection affinity/stickiness should be preserved for established connections. The feature should be configurable through the 3x-ui UI without manually editing the generated Xray configuration. Existing single-level balancers should continue to work exactly as they do now. ### Which parts of the panel would this affect? Frontend (UI / panel pages), Xray config generation, Multi-node (sub-nodes), Other ### Are you willing to help? - [ ] I'd like to implement this feature myself and open a PR. - [x] I can help test once a PR is open. ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this feature has not been requested. - [x] This feature request is written in English.
Author
Owner

@github-actions[bot] commented on GitHub (Aug 28, 2026):

Valid feature request — a balancer cannot currently be used as the next hop of another balancer in one panel, but the exact topology in your diagram is already buildable today with two panels, and I'll show both the workarounds and what the panel is missing.

What exists today (and why it isn't what you asked for)

1. Balancer → balancer exists, but only as fallbackTag (failover), not as a serial hop.

Choosing another balancer in the Fallback field of the balancer form makes the panel write a loopback outbound tagged _bl_<targetBalancer> plus a routing rule that sends it to the target balancer:

// frontend/src/pages/xray/balancers/balancer-loopback.ts:81
const newOutbound = { tag: lbTag, protocol: 'loopback', settings: { inboundTag: lbTag } };
// :103
rules.splice(firstGeneralRoutingRuleIndex(rules), 0, {
  type: 'field', inboundTag: [lbTag], balancerTag: targetBalancerTag,
});

Wired up in BalancersTab.tsx:242-254 and surfaced by BalancerFormModal.tsx:257-264. The panel's own hint says what it does — "fallbackBalancerHint": "Select another balancer as fallback" (internal/web/translation/en-US.json:1888). It landed in 142dab9e — feat(balancer): add balancer-to-balancer fallback support (#5586) — first released in v3.5.0. That is "use balancer B instead of balancer A when A has no usable member", not "send A's traffic through B".

2. Serial chaining exists, but per outbound and by outbound tag only.

sockopt.dialerProxy is exposed on every outbound (frontend/src/pages/xray/outbounds/transport/sockopt.tsx:42-53) and documented as "Chain this outbound's dialing through another outbound tag" (docs/content/docs/en/config/transports.mdx:149). The picker is built purely from outbound tags:

// frontend/src/pages/xray/outbounds/OutboundsTab.tsx:178-189
const dialerProxyTags = useMemo(() => {
  const tags = new Set<string>();
  (templateSettings?.outbounds || []).forEach((o, i) => { ... if (o?.tag) tags.add(o.tag); });

No balancer tag ever reaches it. That is the precise gap your request names.

Two ways to get your topology now

A. Put the second balancer on the RU servers (matches your diagram exactly, no panel change).

In 3x-ui a node is "another 3x-ui panel that your master panel manages over the node's API" and the master syncs inbounds (all or selected by tag), not routing/outbounds — docs/content/docs/en/operations/multi-node.mdx:12-28. So each RU box keeps its own Xray config:

  1. On the entry panel: one leastLoad balancer whose selector covers RU-1/RU-2/RU-3, referenced from your routing rule via balancerTag.
  2. On each RU panel: a second leastLoad balancer whose selector covers DE-1/NL-1/FI-1, referenced from that panel's own routing rule.

This gives Inbound → least-loaded RU → least-loaded foreign → Internet, lets the two tiers scale independently, and each stage picks a member per connection, so a connection stays on the RU server it was assigned. Both tiers need a health monitor for leastLoad (burstObservatory) / leastPing (observatory) — see docs/content/docs/en/operations/outbounds-routing.mdx:147-164.

B. Composite outbounds in a single config (if you must keep one panel).

Create one foreign outbound per (RU, foreign) pair with streamSettings.sockopt.dialerProxy set to the RU outbound tag, then point one balancer at all of them. Honest downside: 3 RU × 3 foreign = 9 outbounds, and a single balancer decides the whole path instead of two independent stages — so it is not equivalent to what you asked for, just the closest single-config approximation.

For the maintainer — most of the machinery is already here

"Balancer as a next hop" would mostly be plumbing rather than a new subsystem:

  • ensureBalancerLoopback() (frontend/src/pages/xray/balancers/balancer-loopback.ts:71-109) already materialises the _bl_<tag> loopback outbound + a routing rule pinned ahead of general rules (ordering fixed in a9d5d9af, #6054 — the infinite-loop regression in #5960).
  • detectBalancerCycles() (balancer-loopback.ts:172-199) and the form's cycle guard (BalancerFormModal.tsx:97-138, blocking submit at BalancerFormModal.tsx:208) already exist for balancer graphs.
  • The backend already maps a balancer tag to its loopback outbound tag for balancer overrides: resolveOverrideTarget in internal/web/service/xray.go:1246.
  • _bl_ is already a reserved tag prefix (frontend/src/schemas/xray.ts:133,144), so hand-rolling the loopback in the Advanced template needs a different tag.

What I could not determine: whether xray-core accepts a protocol: "loopback" outbound as a sockopt.dialerProxy target. The xray-core source is not part of this checkout (pinned at github.com/xtls/xray-core v1.260327.1-0.20260728075948-5ca6f4b7d4dc, go.mod:28) and I cannot run a core here, so I verified nothing beyond the panel's own code. A single manual test — one loopback outbound plus a rule with its inboundTag, referenced as another outbound's dialerProxy — settles whether option B above can be collapsed into "dialerProxy → balancer", or whether this needs a different mechanism entirely.

Not a duplicate. #6305 also asks for more from balancers, but it is about routing rules inside the client/subscription balancer (internal/web/service/sub_balancer.go), a different subsystem — related only.

Verdict: Feature request

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:5458423573 --> @github-actions[bot] commented on GitHub (Aug 28, 2026): Valid feature request — a balancer cannot currently be used as the *next hop* of another balancer in one panel, but the exact topology in your diagram is already buildable today with two panels, and I'll show both the workarounds and what the panel is missing. ## What exists today (and why it isn't what you asked for) **1. Balancer → balancer exists, but only as `fallbackTag` (failover), not as a serial hop.** Choosing another balancer in the **Fallback** field of the balancer form makes the panel write a `loopback` outbound tagged `_bl_<targetBalancer>` plus a routing rule that sends it to the target balancer: ```ts // frontend/src/pages/xray/balancers/balancer-loopback.ts:81 const newOutbound = { tag: lbTag, protocol: 'loopback', settings: { inboundTag: lbTag } }; // :103 rules.splice(firstGeneralRoutingRuleIndex(rules), 0, { type: 'field', inboundTag: [lbTag], balancerTag: targetBalancerTag, }); ``` Wired up in `BalancersTab.tsx:242-254` and surfaced by `BalancerFormModal.tsx:257-264`. The panel's own hint says what it does — `"fallbackBalancerHint": "Select another balancer as fallback"` (`internal/web/translation/en-US.json:1888`). It landed in `142dab9e` — *feat(balancer): add balancer-to-balancer fallback support (#5586)* — first released in **v3.5.0**. That is "use balancer B **instead of** balancer A when A has no usable member", not "send A's traffic **through** B". **2. Serial chaining exists, but per outbound and by outbound tag only.** `sockopt.dialerProxy` is exposed on every outbound (`frontend/src/pages/xray/outbounds/transport/sockopt.tsx:42-53`) and documented as *"Chain this outbound's dialing through another outbound tag"* (`docs/content/docs/en/config/transports.mdx:149`). The picker is built purely from outbound tags: ```ts // frontend/src/pages/xray/outbounds/OutboundsTab.tsx:178-189 const dialerProxyTags = useMemo(() => { const tags = new Set<string>(); (templateSettings?.outbounds || []).forEach((o, i) => { ... if (o?.tag) tags.add(o.tag); }); ``` No balancer tag ever reaches it. That is the precise gap your request names. ## Two ways to get your topology now **A. Put the second balancer on the RU servers (matches your diagram exactly, no panel change).** In 3x-ui a node is *"another 3x-ui panel that your master panel manages over the node's API"* and the master syncs **inbounds** (`all` or `selected` by tag), not routing/outbounds — `docs/content/docs/en/operations/multi-node.mdx:12-28`. So each RU box keeps its own Xray config: 1. On the entry panel: one `leastLoad` balancer whose selector covers `RU-1/RU-2/RU-3`, referenced from your routing rule via `balancerTag`. 2. On **each** RU panel: a second `leastLoad` balancer whose selector covers `DE-1/NL-1/FI-1`, referenced from that panel's own routing rule. This gives `Inbound → least-loaded RU → least-loaded foreign → Internet`, lets the two tiers scale independently, and each stage picks a member per connection, so a connection stays on the RU server it was assigned. Both tiers need a health monitor for `leastLoad` (`burstObservatory`) / `leastPing` (`observatory`) — see `docs/content/docs/en/operations/outbounds-routing.mdx:147-164`. **B. Composite outbounds in a single config (if you must keep one panel).** Create one foreign outbound per (RU, foreign) pair with `streamSettings.sockopt.dialerProxy` set to the RU outbound tag, then point one balancer at all of them. Honest downside: 3 RU × 3 foreign = 9 outbounds, and a single balancer decides the whole path instead of two independent stages — so it is not equivalent to what you asked for, just the closest single-config approximation. ## For the maintainer — most of the machinery is already here "Balancer as a next hop" would mostly be plumbing rather than a new subsystem: - `ensureBalancerLoopback()` (`frontend/src/pages/xray/balancers/balancer-loopback.ts:71-109`) already materialises the `_bl_<tag>` loopback outbound + a routing rule pinned ahead of general rules (ordering fixed in `a9d5d9af`, #6054 — the infinite-loop regression in #5960). - `detectBalancerCycles()` (`balancer-loopback.ts:172-199`) and the form's cycle guard (`BalancerFormModal.tsx:97-138`, blocking submit at `BalancerFormModal.tsx:208`) already exist for balancer graphs. - The backend already maps a balancer tag to its loopback outbound tag for balancer overrides: `resolveOverrideTarget` in `internal/web/service/xray.go:1246`. - `_bl_` is already a reserved tag prefix (`frontend/src/schemas/xray.ts:133,144`), so hand-rolling the loopback in the Advanced template needs a different tag. **What I could not determine:** whether xray-core accepts a `protocol: "loopback"` outbound as a `sockopt.dialerProxy` target. The xray-core source is not part of this checkout (pinned at `github.com/xtls/xray-core v1.260327.1-0.20260728075948-5ca6f4b7d4dc`, `go.mod:28`) and I cannot run a core here, so I verified nothing beyond the panel's own code. A single manual test — one loopback outbound plus a rule with its `inboundTag`, referenced as another outbound's `dialerProxy` — settles whether option B above can be collapsed into "dialerProxy → balancer", or whether this needs a different mechanism entirely. **Not a duplicate.** #6305 also asks for more from balancers, but it is about routing rules *inside* the client/subscription balancer (`internal/web/service/sub_balancer.go`), a different subsystem — related only. Verdict: Feature request *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
Author
Owner

@Lemirda commented on GitHub (Aug 28, 2026):

In my case, I have one Master server with 3x-ui and three separate Russian servers. I also have multiple foreign servers.

What I need is specifically a two-stage load-balancing chain within a single 3x-ui instance:

Client
  ↓
Inbound (Master)
  ↓
RU Balancer
  ├── RU-1
  ├── RU-2
  └── RU-3
       ↓
Foreign Balancer
  ├── DE-1
  ├── NL-1
  └── FI-1
       ↓
    Internet

The first balancer should select the least-loaded Russian server. After that, the traffic should be routed through the selected Russian server, and then the second balancer should select the least-loaded foreign server.

I don't want to run a separate 3x-ui instance or a separate foreign balancer on each Russian server. I want both balancers to be configured on the Master server.

So essentially, what I need is the ability to use a balancer tag as a dialerProxy / next hop, for example:

RU Balancer → selected RU outbound → Foreign Balancer → selected foreign outbound

This would allow the first balancer to choose the entry/relay server and the second balancer to choose the exit server.

If this could be supported in 3x-ui, it would solve my use case completely.

<!-- gh-comment-id:5458453712 --> @Lemirda commented on GitHub (Aug 28, 2026): In my case, I have **one Master server with 3x-ui** and **three separate Russian servers**. I also have multiple foreign servers. What I need is specifically a **two-stage load-balancing chain within a single 3x-ui instance**: ```text Client ↓ Inbound (Master) ↓ RU Balancer ├── RU-1 ├── RU-2 └── RU-3 ↓ Foreign Balancer ├── DE-1 ├── NL-1 └── FI-1 ↓ Internet ``` The first balancer should select the least-loaded Russian server. After that, the traffic should be routed **through the selected Russian server**, and then the second balancer should select the least-loaded foreign server. I don't want to run a separate 3x-ui instance or a separate foreign balancer on each Russian server. I want both balancers to be configured on the Master server. So essentially, what I need is the ability to use a **balancer tag as a `dialerProxy` / next hop**, for example: ```text RU Balancer → selected RU outbound → Foreign Balancer → selected foreign outbound ``` This would allow the first balancer to choose the entry/relay server and the second balancer to choose the exit server. If this could be supported in 3x-ui, it would solve my use case completely.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#4313
No description provided.