美文网首页技术nginx
nginx模块stream配置

nginx模块stream配置

作者: 前浪浪奔浪流 | 来源:发表于2022-05-19 10:20 被阅读0次

    一、stream模块概要。

    stream模块一般用于tcp/UDP数据流的代理和负载均衡,可以通过stream模块代理转发TCP消息。 ngx_stream_core_module模块由1.9.0版提供。 默认情况下,没有构建此模块。 -必须使用-with stream配置参数启用。 也就是说,必须在使用./configure --with-stream编译时添加流模块。 流模块的使用方法与http模块相同,语法也基本相同。

    二、使用场景说明stream主要有两个可用场景。

    一是实现流量的代理转发。 这里所述的代理转发是指,只有一些端口服务被限制为活动IP地址。 例如,mysql账户一般将源地址限制为APP应用服务器,而nginx可能同时是web APP应用服务器。 开发人员需要验证一些数据库数据问题,但帐户的源地址有限制。 此时,通过在nginx中进行流传送,可以实现从开发终端向mysql的访问。 二是实现流量负载均衡。 有多个tcp或udp端口服务,如DNS。 流模块支持负载平衡算法,如轮询、最小连接数和ip_hash,从而实现数据流负载平衡。

    三、配置实例

    开启stream

    修改/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;
    }
    
    新增一个Stream代理

    添加7000端口的stream代理配置。
    新增配置/etc/nginx/conf.d/proxy_port7000_to_59.110.xxx.xxxp8001.stream

    server{
        listen 7000;
        proxy_pass 59.110.xxx.xxx:8001;
    }
    
    重新加载配置

    测试一下配置文件写的是否有问题
    shell> nginx -t
    配置文件没问题的话,重新加载配置
    shell> nginx -s reload
    例子:

    
    user  nginx;
    worker_processes  4;
    worker_cpu_affinity 0001 0010 0100 1000;
    
    worker_rlimit_nofile 65535;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {
        use epoll;
        worker_connections  102400;
        accept_mutex on;
        multi_accept on;
           }
    
    http {
        include       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  logs/access.log  main;
        log_format format1 '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $http_x_forwarded_for $upstream_cache_status $request_time';
        access_log  logs/access.log  format1;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 4k;
        large_client_header_buffers 4 8k;
        client_max_body_size 8m;
        #sendfile       on;
        #tcp_nopush     on;
        keepalive_timeout  60s;
        #tcp_nodelay on;
        client_header_timeout 25s;
        client_body_timeout 25s;
        send_timeout 25s;
    
        gzip  on;
        gzip_min_length 2k;
        gzip_buffers 4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 7;
        gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png image/ico;
        gzip_vary off;
        gzip_disable "MSIE [1-6]\.";
        proxy_headers_hash_max_size 51200;
        proxy_headers_hash_bucket_size 6400;
        proxy_buffer_size 128k;
        proxy_buffers   32 128k;
        proxy_busy_buffers_size 128k;
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
    
    
        server {
            listen       80;
            server_name  localhost;
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            location /nginx_status
            {
                    stub_status on;
                    access_log off;
            }
       
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
                                           }
     }
    
        # 各个域名单独控制
        include conf.d/*.conf;
    }
    
    
    stream {
        server {
            listen 9898;
            proxy_pass 192.168.0.202:9999;
               }  
           }
    

    相关文章

      网友评论

        本文标题:nginx模块stream配置

        本文链接:https://www.haomeiwen.com/subject/znxtprtx.html