nginx module stream configuration

1. Overview of the stream module.

The stream module is generally used for proxying and load balancing of tcp/UDP data streams, and can forward TCP messages through the proxy of the stream module. The ngx_stream_core_module module is provided by version 1.9.0. By default, this module is not built. - Must be enabled with the -with stream configuration parameter. That is, the stream module must be added when compiling with ./configure --with-stream. The usage method of the stream module is the same as that of the http module, and the syntax is basically the same.

2. Usage Scenario Description Stream has two main available scenarios.

One is to realize proxy forwarding of traffic. Proxy forwarding as described here means that only some port services are restricted to active IP addresses. For example, the mysql account generally restricts the source address to the APP application server, while nginx may also be the web APP application server. Developers need to verify some database data issues, but there are restrictions on the source address of the account. At this point, access from the development terminal to mysql can be realized by streaming in nginx. The second is to achieve traffic load balancing. There are multiple tcp or udp port services like DNS. The streaming module supports load balancing algorithms, such as round robin, minimum number of connections, and ip_hash, so as to achieve data flow load balancing.

3. Configuration example

open stream

Modify /etc/nginx/nginx.conf

#增加stream配置,开启stream模块
http{
xxxxxxxxxx
       }
#stream模块和http模块是并列级别的,所以stream要写在http{}外边
stream {
    log_format basic '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time';
    access_log /var/log/nginx/stream-access.log basic buffer=32k;
    # 为了让这个配置文件简单一些,将配置stream放入到/etc/nginx/conf.d,并以.stream做后缀名。
    # 需要为每个端口创建一个.stream做后缀名的配置文件
    include /etc/nginx/conf.d/*.stream;
}

Guess you like

Origin blog.csdn.net/qq_34412985/article/details/130933735