Nginx端口转发


0. 在http配置段下面,非http配置段内

TCP端口转发

1
2
3
4
5
6
7
8
stream {  
server {
listen 23454;
proxy_connect_timeout 30s;
proxy_timeout 5m;
proxy_pass 1.2.3.4:3389;
}
}

UDP端口转发

1
2
3
4
5
6
7
8
stream {  
server {
listen 53 udp;
proxy_connect_timeout 30s;
proxy_timeout 5m;
proxy_pass 16.162.28.228:53;
}
}

1. 端口转发的负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
stream {
upstream my_servers {
least_conn;
server <IP_SERVER_1>:3306 max_fails=3 fail_timeout=5s;
server <IP_SERVER_2>:3306 max_fails=3 fail_timeout=5s;
server <IP_SERVER_3>:3306 max_fails=3 fail_timeout=5s;
}

server {
listen 3306;
proxy_connect_timeout 5s;
proxy_timeout 10s;
proxy_next_upstream on;
proxy_next_upstream_tries 3;
proxy_next_upstream_timeout 10s;
proxy_socket_keepalive on;
proxy_pass my_servers;
}
}