前言
大家通常使用,nginx作为http层的7层代理软件。无意间发现nginx居然也能支持4层代理。
支持说明
官方文档:http://nginx.org/en/docs/stream/ngx_stream_core_module.html
四层代理依赖模块ngx_stream_core_module,该模块自1.9.0版开始可用。
可以使用nginx -V查看,有–with-stream模块表示支持
[root@localhost nginx]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1g FIPS 21 Apr 2020 (running with OpenSSL 1.1.1k FIPS 25 Mar 2021)
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --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_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
1.20.1版本默认有--with-stream=dynamic模块,直接支持4层代理。但是是动态库,需要再执行安装。
yum安装stream模块
nginx 用yum源安装的,stream模块也用yum源安装
安装stream模块动态模块
shell> yum install -y nginx-mod-stream
安装所有的动态模块
shell>yum install -y nginx-all-modules
查看安装后的动态模块
[root@localhost nginx]# ls /usr/lib64/nginx/modules
ngx_stream_module.so
手动编译安装stream模块
参考:yum安装下的nginx,如何添加模块,和添加第三方模块
Nginx4层代理配置相关
在nginx.conf文件中使用stream模块来配置4层代理,该模块与http模块平级
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
stream { #stream模块 和http模块是一同等级;做四层代理时需要添加上这个模块;
server {
listen 40028; #40028端口将以4层TCP协议方式转发至后端app_sever;
proxy_pass app_server;
}
upstream app_server{
server 192.168.1.100:3306;
}
server {
listen 40030; #40030端口将以4层TCP协议方式转发至后端app_sever;
proxy_pass mongo;
}
upstream mongo{
server 192.168.1.101:27017;
}
}