通过 nginx 动态加载 stream 模块

本贴最后更新于 2540 天前,其中的信息可能已经时过境迁

Ngix1.9.11 开始增加加载动态模块支持,从此不再需要替换 nginx 文件即可增加第三方扩展。目前官方只有几个模块支持动态加载,第三方模块需要升级支持才可编译成模块。
我们测试下通过 nginx 动态加载模块,添加 stream 模块实现 tcp 反向代理功能。

查看支持
./configure --help | grep dynamic --with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module --with-http_image_filter_module=dynamic enable dynamic ngx_http_image_filter_module --with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module --with-http_perl_module=dynamic enable dynamic ngx_http_perl_module --with-mail=dynamic enable dynamic POP3/IMAP4/SMTP proxy module --with-stream=dynamic enable dynamic TCP/UDP proxy module --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module --add-dynamic-module=PATH enable dynamic external module --with-compat dynamic modules compatibility
动态加载 stream 模块
nginx -V nginx version: nginx/1.12.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2l 25 May 2017 TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-openssl=/home/packet/lnmp1.4-full/src/openssl-1.0.2l

在原始参数后面添加 --with-stream=dynamic

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-openssl=/home/packet/lnmp1.4-full/src/openssl-1.0.2l --with-stream=dynamic

make 编译 ,切记不要 install
当前目录 objs 中会存在 编译好的 ngx_stream_module.so

cp ngx_stream_module.so nginx_worker_dir/modules/
更改配置
load_module modules/ngx_stream_module.so; stream { upstream proxy_name { server 127.0.0.1:9553; } server { listen 8088; proxy_pass proxy_name; } }

reload nginx 测试 stream 是否生效

  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    315 引用 • 547 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...