2024年3月28日星期四

OpenResty/Nginx设置TCP/UDP端口转发

TCP/UDP端口转发配置示例:
~/nginx.conf文件末尾添加配置代码:


stream {
include /etc/nginx/conf.d/*.stream;
}


stream {
include /usr/local/openresty/nginx/conf/conf.d/*.stream;
}

conf.d目录下,添加*.stream后缀的配置文件,并编辑配置:

tcp.stream 文件:
#同时监听ipv4及ipv6的88端口并转发到本地localhost的80端口
server {
listen 88;
listen [::]:88;
proxy_connect_timeout 5s;
proxy_timeout 20s;
proxy_pass localhost:80;
}
#将udp 53端口转发到192.168.1.23 53端口
server {
listen 88 udp reuseport;
listen [::]:88 udp reuseport;
proxy_timeout 20s;
proxy_pass localhost:80;
}