nginx自从1.9.0版本开始已经有4层代理了,编译安装需要添加参数:
--with-stream
四层代理的标签是stream,类似http,不能位于http里面,简单配置可以参考以下:
stream {
server {
listen 10000;
proxy_pass 127.0.0.1:10000;
}
server {
listen 8000;
proxy_pass 127.0.0.1:8000;
}
}
多台可以均衡:
stream {
upstream stream_backend {
least_conn;
server backend1.example.com:12345 weight=5;
server backend2.example.com:12345 max_fails=2 fail_timeout=30s;
server backend3.example.com:12345 max_conns=3;
}
upstream dns_servers {
least_conn;
server 192.168.136.130:53;
server 192.168.136.131:53;
server 192.168.136.132:53;
}
server {
listen 12345;
proxy_pass stream_backend;
proxy_timeout 3s;
proxy_connect_timeout 1s;
}
server {
listen 53 udp;
proxy_pass dns_servers;
}
server {
listen 12346;
proxy_pass backend4.example.com:12346;
}
}
具体可以参考官方文档:
https://docs.nginx.com/nginx/admin-guide/load-balancer/tcp-udp-load-balancer/
http://nginx.org/en/docs/stream/ngx_stream_core_module.html
原文链接:nginx 四层代理配置,转载请注明来源!