官方文档:http://nginx.org/en/docs/stream/ngx_stream_core_module.html
nginx四层代理需要ngx_stream_core_module模块支持,这个模块需要在nginx1.9版本之后。
默认这个模块不会编译,需要加编译参数–with-stream
[root@localhost nginx-1.17.3]# ./configure --prefix=/usr/local/nginx --with-stream
# 主配置文件通过include指令引入其他目录下的配置文件,便于管理。
[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } } # 注意这个include是在http{}配置段外层,也就是说stream和http段是平级的。 include vhost/*.conf;
# 四层代理配置
[root@localhost conf]# cat vhost/a.conf stream { upstream ssh { server 192.168.10.74:22; } server { listen 8888; proxy_pass ssh; } }
这样ssh登陆本机8888端口时,就会连接74这台机器的22端口。
upstream中可以写多个server,也支持会话保持。
会话保持参考官方文档:http://nginx.org/en/docs/stream/ngx_stream_upstream_module.html#hash
原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/4523.html