这里介绍了TCP转发、以及应用。如果不使用Nginx实现,可参考:
Nginx配置端口转发需要开启nginx的模块--with-stream,官网:https://nginx.org/en/docs/stream/ngx_stream_core_module.html。该ngx_stream_core_module模块自 1.9.0 版本起可用。该模块默认不会构建的,需要通过 --with-stream 配置参数启用。
Nginx安装编译的时候追加参数 --with-stream 编译完成。
查看可通过 nginx -V 结果有:--with-stream 就算有!
nginx -V
查看有 --with-stream 就算有这个模块
zanglikun@zanglikundeMacBook-Pro-2 ~ % nginx -V
nginx version: nginx/1.27.5
built by clang 16.0.0 (clang-1600.0.26.6)
built with OpenSSL 3.4.1 11 Feb 2025 (running with OpenSSL 3.5.0 8 Apr 2025)
TLS SNI support enabled
configure arguments: --prefix=/opt/homebrew/Cellar/nginx/1.27.5 --sbin-path=/opt/homebrew/Cellar/nginx/1.27.5/bin/nginx --with-cc-opt='-I/opt/homebrew/opt/pcre2/include -I/opt/homebrew/opt/openssl@3/include' --with-ld-opt='-L/opt/homebrew/opt/pcre2/lib -L/opt/homebrew/opt/openssl@3/lib' --conf-path=/opt/homebrew/etc/nginx/nginx.conf --pid-path=/opt/homebrew/var/run/nginx.pid --lock-path=/opt/homebrew/var/run/nginx.lock --http-client-body-temp-path=/opt/homebrew/var/run/nginx/client_body_temp --http-proxy-temp-path=/opt/homebrew/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/opt/homebrew/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/opt/homebrew/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/opt/homebrew/var/run/nginx/scgi_temp --http-log-path=/opt/homebrew/var/log/nginx/access.log --error-log-path=/opt/homebrew/var/log/nginx/error.log --with-compat --with-debug --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-ipv6 --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
在配置文件追加 stream块 (写在http块外面即可),重启即生效!!!
upstream backend_servers { # 定义后端服务器组,移至顶层
server backend1.example.com;
server backend2.example.com;
}
http {
include mime.types; # 引入 MIME 类型配置
default_type application/octet-stream;
server { # 可忽略server的配置
listen 80;
server_name example.com;
location / { # 定义路径匹配
root html;
index index.html index.htm;
}
location /api { # 另一个路径匹配
proxy_pass http://backend_servers; # 转发到 upstream
}
}
}
stream { # 定义 TCP/UDP 流量处理 (修正缩进)
server {
listen 12345;
proxy_pass backend_servers; # 转发到 upstream
}
}
真实演示
放心下面配置文件不会有错误!!!
如果不生效,请检查:nginx -t -c /xxx/nginx.conf。
这里建议使用nginx配置文件为绝对路径,不要使用相对路径,我使用源码编译过一次,编译好了后,我修改半天编译前的conf/nginx.conf,无论怎么重启,怎么修改,都没生效。因为启动后,没有使用我修改的配置文件,
建议直接使用 nginx -s reload -c /xxx/nginx.conf !一劳永逸!!!
# 特殊说明,nginx下面配置文件没问题,如果复制出现了问题,请第一时间确认空格问题!
stream {
upstream myudp {
server 119.91.119.192:54230;
server game.ff11sf.com:54230;
}
# UDP配置
server {
listen 54230 udp;
proxy_responses 1; # 只是控制缓存后端服务器的响应,1开启,0关闭。可不配置
proxy_pass myudp;
}
# TCP配置
server {
listen 54230;
proxy_responses 1;
proxy_pass 119.91.119.192:54230;
}
server {
listen 54231;
proxy_responses 1;
proxy_pass 119.91.119.192:54231;
}
server {
listen 54001;
proxy_responses 1;
proxy_pass 119.91.119.192:54001;
}
server {
listen 54002;
proxy_responses 1;
proxy_pass 119.91.119.192:54002;
}
}
http {
}
特殊说明:
上述文章均是作者实际操作后产出。烦请各位,请勿直接盗用!转载记得标注原文链接:www.zanglikun.com
第三方平台不会及时更新本文最新内容。如果发现本文资料不全,可访问本人的Java博客搜索:标题关键字。以获取最新全部资料 ❤
免责声明: 本站文章旨在总结学习互联网技术过程中的经验与见解。任何人不得将其用于违法或违规活动!所有违规内容均由个人自行承担,与作者无关。
第三方平台不会及时更新本文最新内容。如果发现本文资料不全,可访问本人的Java博客搜索:标题关键字。以获取最新全部资料 ❤
免责声明: 本站文章旨在总结学习互联网技术过程中的经验与见解。任何人不得将其用于违法或违规活动!所有违规内容均由个人自行承担,与作者无关。
