zoukankan      html  css  js  c++  java
  • nginx的stream模块和upstream模块

    nginx7层调度方式

    使用upstream模块定义集群名称和节点地址 定义在server字段之外httpd字段之内

    upstream staticweb {

                    server 172.17.0.2; #也可以指定weight=2 指定权(默认为轮询算法rr)

                    server 172.17.0.3;

    }

    server {

            listen 8088;

            server_name www.stephenzhong.com;

            proxy_set_header X-Real-IP $remote_addr;

            location / {

                    proxy_pass http://staticweb; #代理至集群名称

            }


     

    ip_hash使用ip_hash对后端服务器权重取模。然后调度到同一台服务器

    一致性hash算法,对固定数值取模

    upstream staticweb {

                    hash $remote_addr consistent; #一致性hash即使权重之和出问题也不会调度也不会乱套

                    server 172.17.0.2 weight=2;

                    server 172.17.0.3;

    hash $request_uri consistent; $对uri做一致性hash计算。绑定什么意为这什么不变

    2、server address [parameters];

    在upstream上下文中server成员,以及相关的参数;Context: upstream

    address的表示格式:

    unix:/PATH/TO/SOME_SOCK_FILE

    IP[:PORT]

    HOSTNAME[:PORT]

    parameters:

    weight=number

    权重,默认为1;

    max_fails=number

    失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用;

    fail_timeout=time

    设置将服务器标记为不可用状态的超时时长;

    max_conns

    当前的服务器的最大并发连接数;

    backup

    将服务器标记为“备用”,即所有服务器均不可用时此服务器才启用;

    down

    标记为“不可用”;

    3、least_conn;

    最少连接调度算法,当server拥有不同的权重时其为wlc;

    4、 ip_hash;

    源地址hash调度方法;

    5、hash key [consistent];

    基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者的组合;

    作用:将请求分类,同一类请求将发往同一个upstream server;

    If the consistent parameter is specified the ketama consistent hashing method will be used instead.

    示例:

    hash $request_uri consistent;

    hash $remote_addr;

    6、keepalive connections;

    为每个worker进程保留的空闲的长连接数量;

    upstream memcached_backend {

        server 127.0.0.1:11211;

        server 10.0.0.2:11211;

        keepalive 32;

    }

    server {

        ...

        location /memcached/ {

            set $memcached_key $uri;

            memcached_pass memcached_backend;

        }

    }

    server 172.17.0.2 weight=2 fail_timeout=2 max_fails=2;#定义后端监控状态失败超时时长2秒最大测试连接次数2秒

     server 172.17.0.3 weight=2 fail_timeout=2 max_fails=2 backup;

    nginx stream 模块

    方向代理mysql

    docker run --name db1 -d -e "MYSQL_ROOT_PASSWORD=123456" -v /vols/db1:/var/lib/mysql mysql:5.7 #启动mysql容器

    docker exec -it db1 /bin/sh #连接容器设置远程连接账户

    grant all on wpdb.* to wpuser@'%' identified by '123456'

    vim /etc/nginx/nginx.conf 将stream定义在http字段之外。

    stream {

            server {

                    listen 3306;

                    proxy_pass 172.17.0.4:3306;

            }

    }

    [root@centos7 nginx]# mysql -uwpuser -h192.168.1.198 -p123456

    Welcome to the MariaDB monitor.  Commands end with ; or g.

    Your MySQL connection id is 3

    Server version: 5.7.27 MySQL Community Server (GPL)

    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

    MySQL [(none)]>

    ########################定义四层调度的负载集群

    upstream backend {

     hash $remote_addr consistent; 

     server backend1.example.com:12345 weight=5; 

     server backend2.example.com:12345; 

     server unix:/tmp/backend3; 

     server backup1.example.com:12345 backup;

     server backup2.example.com:12345 backup;

    }

    server { 

     listen 12346; 

     proxy_passbackend;

    }

    nginx负载均衡优化

    net.ipv4.tcp_fin_timeout=30 当服务器主动关闭连接时,设定的超时时长

    net.ipv4.tcp_max_tw_buckets=8000,允许time_wait套接字数量的较大值,建议8000

    net.ipv4.ip_local_port_range=1024 65000 udp和tcp端口取值范围

    net.ipv4.tcp_syncookies=1 与性能无关用于解决tcp的syn攻击

    net.ipv4.tcp_max_syn_backlog=8192 接收消息队列最大数值

    net.ipv4.tcp_tw_recycle=1 用于timewait快速回收

    net.ipv4.tcp_max_orphans=262114 限制放置简单的dos攻击

    对客户端进行限制的相关配置:

    18、limit_rate rate;

    限制响应给客户端的传输速率,单位是bytes/second,0表示无限制;

    19、limit_except method ... { ... }

    限制对指定的请求方法之外的其它方法的使用客户端;

    limit_except GET {

    allow 192.168.1.0/24;

    deny  all;

    }

    文件操作优化的配置

    20、aio on | off | threads[=pool];

    是否启用aio功能;

    21、directio size | off;

    在Linux主机启用O_DIRECT标记,此处意味文件大于等于给定的大小时使用,例如directio 4m;

    22、open_file_cache off;

    open_file_cache max=N [inactive=time];

    nginx可以缓存以下三种信息:

    (1) 文件的描述符、文件大小和最近一次的修改时间;

    (2) 打开的目录结构;

    (3) 没有找到的或者没有权限访问的文件的相关信息;

    max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现缓存管理;

    inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项;

    23、open_file_cache_valid time;

    缓存项有效性的检查频率;默认为60s;

    24、open_file_cache_min_uses number;

    在open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次方可被归类为活动项;

    25、open_file_cache_errors on | off;

    是否缓存查找时发生错误的文件一类的信息;

  • 相关阅读:
    POJ 2155 Matrix(二维树状数组)
    HDU 1280 前m大的数
    HDU 3183 A Magic Lamp(二维RMQ)
    HDU 3743 Frosh Week(归并排序求逆序数)
    POJ 2299 Ultra-QuickSort ( 归并排序 + 求逆序数 )
    HDU 1166 敌兵布阵(树状数组)
    HDU 2846 Repository(字典树)
    HDU 1896 Stones(优先队列)
    HDU 4393 Throw nails(优先队列)
    进程池
  • 原文地址:https://www.cnblogs.com/woaiyitiaochai/p/11778291.html
Copyright © 2011-2022 走看看