I had one V2Ray client running in a container on Ubuntu. I wanted to start a second copy with a different configuration and give both clients some of the work. The first example I saw put Nginx on port 443 and listed two V2Ray ports behind it. Ubuntu already had a Network Proxy panel in GNOME, its desktop environment. If I entered an address there, would that do the same thing?
Who chooses which client gets a connection?
A proxy accepts a connection from an application and makes an onward connection for it. With one local client, the browser needs just two numbers: an address such as 127.0.0.1, which means “this computer,” and a port, which identifies the program listening there. GNOME’s manual proxy settings let you save one such address and port for each protocol. An application that reads those settings knows where to connect. Entering client A in the HTTP field and client B in the HTTPS field assigns them different kinds of traffic. Every new connection of the same kind still gets the same setting.1
What I wanted was client-side load balancing. Both V2Ray processes would run on my Ubuntu machine. Each would accept local connections and use its own configuration to reach a remote proxy. For an application to use both through one address, something on Ubuntu has to accept each new connection and choose a client for it. Nginx can do that job locally with its stream module.2
flowchart LR
App["Browser using GNOME settings"] --> N["Local Nginx"]
N --> A["Local V2Ray A"]
N --> B["Local V2Ray B"]
What address goes into GNOME?
Suppose both V2Ray clients accept HTTP proxy connections, the protocol a browser can use to ask a proxy for a web page or for a tunnel to a secure site. Client A listens on local port 10000 and B on 10001. Here is the earlier Nginx idea adapted for the desktop. It assumes both clients are reachable at those ports from the host and Nginx has its stream module loaded. The block belongs at the top level of the Nginx configuration, alongside any http block.
stream {
upstream local_clients {
server 127.0.0.1:10000;
server 127.0.0.1:10001;
}
server {
listen 127.0.0.1:18080;
proxy_pass local_clients;
}
}
Once that configuration is active, open Network Proxy → Manual. For both HTTP and HTTPS destinations, enter 127.0.0.1 as the proxy host and 18080 as the port. A browser using the desktop settings now reaches Nginx first. Nginx forwards the bytes to one V2Ray client, so both clients must understand the proxy protocol the browser sends. A SOCKS-only client, for example, expects a different opening exchange.
With two available clients of equal weight, Nginx uses round robin to distribute new TCP connections. TCP is the connection carrying the bytes between programs. A long download on one connection stays with the client chosen when it opened. A browser can also reuse one connection for several requests, so reloading a page twice is a poor test of the distribution.2
How would I tell whether both clients got work?
First, try the same destination through each client individually. For example, run curl --noproxy '' --proxy http://127.0.0.1:10000 https://example.com/, then change the port to 10001. The explicit proxy makes each test independent of the desktop settings. Once both work, use port 18080 and run the command several times as separate processes to open fresh connections. To see the actual choice, add these two lines inside stream and reload the configuration before testing:3
log_format client_choice '$upstream_addr';
access_log /var/log/nginx/client-choice.log client_choice;
After the connections close, look for both 127.0.0.1:10000 and 127.0.0.1:10001 in that log. On my machine the second client exited at startup, before I could verify that both clients received connections. I traced that failure in Why did my second V2Ray container exit with code 23 after I changed its port?
Footnotes
-
GNOME Project. (n.d.). Define proxy settings. GNOME Help, “Change proxy method”. See also the manual proxy keys in gsettings-desktop-schemas 46.0. ↩
-
NGINX. (n.d.). Module ngx_stream_upstream_module. NGINX documentation,
upstreamdirective and default connection distribution. ↩ ↩2 -
NGINX. (n.d.). Module ngx_stream_log_module. NGINX documentation,
log_format. The upstream module describes the value recorded by$upstream_addr. ↩