mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #6355] [Feature]: Support chained load balancers for multi-stage traffic routing #4313
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#4313
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
Second balancer
Name: foreign-balancer
Strategy: leastLoad
Nodes:
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?
Before submitting
@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
loopbackoutbound tagged_bl_<targetBalancer>plus a routing rule that sends it to the target balancer:Wired up in
BalancersTab.tsx:242-254and surfaced byBalancerFormModal.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 in142dab9e— 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.dialerProxyis 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: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 (
allorselectedby tag), not routing/outbounds —docs/content/docs/en/operations/multi-node.mdx:12-28. So each RU box keeps its own Xray config:leastLoadbalancer whose selector coversRU-1/RU-2/RU-3, referenced from your routing rule viabalancerTag.leastLoadbalancer whose selector coversDE-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 forleastLoad(burstObservatory) /leastPing(observatory) — seedocs/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.dialerProxyset 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 ina9d5d9af, #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 atBalancerFormModal.tsx:208) already exist for balancer graphs.resolveOverrideTargetininternal/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 asockopt.dialerProxytarget. The xray-core source is not part of this checkout (pinned atgithub.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 itsinboundTag, referenced as another outbound'sdialerProxy— 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.
@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:
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: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.