问题原文
原帖链接 · joels · CC BY-SA 3.0
With nginx http directive, you can have multiple servers on the same port with different names:
server {
listen 80;
server_name server1.example.com;
location / {
proxy_pass http://server1.example.com;
}
}
server {
listen 80;
server_name server2.example.com;
location / {
proxy_pass http://server2.example.com;
}
}
Is it possible to have nginx proxy multiple mysql servers on the same port with different names like you can with http? They aren't part of a cluster or anything. The have different, unrelated tables.
stream {
upstream db1.example.com {
server db1.example.com:3306;
#server_name db1.example.com; "server_name" directive is not allowed here
}
upstream db2.example.com {
server db2.example.com:3306;
}
server {
listen 3306;
proxy_pass db1.example.com;
}
#duplicate "3306" address and port pair
#server { listen 3306; proxy_pass db2.example.com; }
}
社区高分解答
原帖链接 · 2ps · CC BY-SA 3.0
Yes, if nginx was compiled with --with-stream. https://www.nginx.com/resources/admin-guide/tcp-load-balancing/
Here is one of the examples with the same port:
upstream stream_backend {
hash $remote_addr;
server backend1.example.com:12345;
server backend2.example.com:12345;
server backend3.example.com:12346;
}
Second update:
You obviously can’t do anything like this:
stream {
upstream db1.example.com {
server db1.example.com:3306;
#server_name db1.example.com; "server_name" directive is not allowed here
}
upstream db2.example.com {
server db2.example.com:3306;
}
server {
listen 3306;
proxy_pass db1.example.com;
}
#duplicate "3306" address and port pair
#server { listen 3306; proxy_pass db2.example.com; }
}
Because the nginx proxy for upstream db1.example.com is competing with db2.example.com for packets on port 3306. So you have to have the proxy for db1.example.com listening on another port than the proxy for db2.example.com. Otherwise nginx wouldn’t know how to route packets to and from the two upstreams. Apologies for misunderstanding your original post. server_name isn't allowed in stream definitions because, unlike http headers, there is no additional metadata in the tcp/udp packet that identifies which DNS was used to address the packet over to nginx.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
