zoukankan      html  css  js  c++  java
  • nginx模块之ngx_http_upstream_module

    ngx_http_upstream_module

    示例:

    http上下文:

    upstream upservers{
        ip_hash; //根据客户端IP进行调度,每个客户端ip地址访问时每个ip生成一个hash码,来自同一个客户端的请求分配到同一个server 
       server 192.168.1.102 weight=2;   
       server 192.168.1.103;
    }

     server端:

    proxy_pass http://upservers/;   

    健康状况检测:

    max_fails=numbers  //检查出的错误次数超过多少次就标记为失败了
    fail_timeout=time  //每此检查的超时时长

     示例:

     upstream upservers {
       server 192.168.1.102 max_fails=2 fail_timeout=1;
     }

     如果要对服务器进行更新,可以这么做:

    upstream upservers {
      server 192.168.1.103 max_fails=2 fail_timeout=1 backup;   //backup: 指定一个服务器为备用服务器 
    }

     ip_hash是基于源IP进行session绑定

    基于sticky进行session绑定:

    格式:

    Syntax:    sticky cookie name [expires=time] [domain=domain] [httponly] [secure] [path=path];
           sticky route $variable ...;
           sticky learn create=$variable lookup=$variable zone=name:size [timeout=time] [header] [sync];
    Default:    —
    Context:    upstream
    This directive appeared in version 1.5.7.

    cookie:

    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
    
        sticky cookie srv_id expires=1h domain=.example.com path=/;
    }

    route:

    map $cookie_jsessionid $route_cookie {
        ~.+.(?P<route>w+)$ $route;
    }
    
    map $request_uri $route_uri {
        ~jsessionid=.+.(?P<route>w+)$ $route;
    }
    
    upstream backend {
        server backend1.example.com route=a;
        server backend2.example.com route=b;
    
        sticky route $route_cookie $route_uri;
    }

     learn:

    upstream backend {
       server backend1.example.com:8080;
       server backend2.example.com:8081;
    
       sticky learn
              create=$upstream_cookie_examplecookie
              lookup=$cookie_examplecookie
              zone=client_sessions:1m;
    }

     least_conn: 调度方法,最少连接

     keepalive: 代理服务器和upstream server之间保持连接,一般后端是http server不建议使用,如果是缓存服务器可以考虑

     health_check:

      建议:关闭访问日志

    自定义响应首部:

    add_header X-Via $server_addr;
    add_header X-Cache $upstream_cache_status;
  • 相关阅读:
    5.19 省选模拟赛 T1 小B的棋盘 双指针 性质
    5.15 省选模拟赛 容斥 生成函数 dp
    5.15 省选模拟赛 T1 点分治 FFT
    5.15 牛客挑战赛40 B 小V的序列 关于随机均摊分析 二进制
    luogu P4929 【模板】舞蹈链 DLX
    CF 878E Numbers on the blackboard 并查集 离线 贪心
    5.10 省选模拟赛 拍卖 博弈 dp
    5.12 省选模拟赛 T2 贪心 dp 搜索 差分
    5.10 省选模拟赛 tree 树形dp 逆元
    luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树
  • 原文地址:https://www.cnblogs.com/ckh2014/p/10875052.html
Copyright © 2011-2022 走看看