nginx 负载均衡

upstream 配置

在http模块
upstream app {

server 192.168.0.250:8081 weight=5;
server www.cn;
server unix:/tmp/app;

server w1.cn:8082 backup;
server w2.cn:8083 backup;

}

server配置参数

 

指令

含义

down

当前的server暂时不参与负载均衡

backup

预留的备份服务器(一旦其它的服务器均宕机没有存活的了,该标记的机器就会接收请求)

max_fails

设置在fail_timeout时间内尝试对一个服务器连接的最大次数,如果超过这个次数,那么就会标记为down,即允许请求失败的次数

fail_timeout

某个server连接失败了max_fails次,则nginx会认为该server不工作了

同时,在接下来的 fail_timeout时间内,nginx不再将请求分发给失效的server。过了fail_timeout会再次检查服务是否恢复。默认:fail_timeout为10s,max_fails为1次

max_conns

限制最大的接收的连接数

nginx负载均衡配置实例
分别监听8081,8082,8083
code1,code2,code3下分别有

index.php

<?php
echo "<pre>";
print_r($_SERVER);
?>

code1.conf

server {
                listen       8081;
                server_name  192.168.0.250:8081;
                root    /mnt/hgfs/www/web/code1;
                autoindex on;

                access_log  /mnt/hgfs/www/log/w1.access.log  main;

                location / {
                                index   index.php;
                                autoindex on;
                                autoindex_exact_size off;
                                autoindex_localtime on;
                }
                error_log /mnt/hgfs/www/log/w1_err.log info;
                error_page  404              /404.html;

                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                                root   html;
                }

                location ~ \.php$ {
                                fastcgi_pass   127.0.0.1:9000;
                                fastcgi_index  index.php;
                                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                                include        fastcgi_params;
                }
 }

nginx.conf里部分

 http里

upstream code{
          server 192.168.0.250:8081;
          server 192.168.0.250:8082;
          server 192.168.0.250:8083;
          server 192.168.0.251;
        }
server {
        listen       80;
        server_name    192.168.0.250 ;
        access_log  logs/host.access.log  main;
        location / {
                proxy_pass http://code;
            }
}

注意输出的 http_host都是code
[HTTP_HOST] => code
[SERVER_NAME] => 192.168.0.250:8083
[SERVER_PORT] => 8083
[SERVER_ADDR] => 192.168.0.250

默认的方式是轮询
阻止8082 的 如果用同一进程启动的,用端口关掉模拟
iptables -I INPUT -p tcp --dport 8082 -j DROP
开启
iptables -I INPUT -p tcp --dport 8082 -j ACCEPT
这种在从8081到8082切换后有延迟相应
下面这种没有
或者关闭 192.168.0.251上的机器上Nginx进程

#演示
upstream code{
          server 192.168.0.250:8081 down;
          server 192.168.0.250:8082 down;
          server 192.168.0.251 max_fails=1 fail_timeout=10s;
          server 192.168.0.250:8083 backup;
        }

停掉 server 192.168.0.251
server 192.168.0.250:8083 立即开始服务
此时立即开启 192.168.0.251 如果还没到10s,访问的还是 192.168.0.250:8083
到10s后 检测到 192.168.0.251服务恢复,192.168.0.250:8083 转变为backup状态 不在提供服务,由192.168.0.251提供服务

 调度算法

轮询

按时间顺序逐一分配到不同的后端服务器

加权轮询

weight值越大,分配到的访问几率越高

ip_hash

每个请求按照固定的ip的hash结果分配,这样来自同一ip的固定访问到一个后端服务器,确保客户端均匀地连接所有服务器,键值基于C类地址

url_hash

按照访问的url的hash结果分配请求,是每个url定向到同一个后端服务器

least_conn

最少连接数,哪个机器连接数少就分发哪个机器

hash关键数值

hash自定义key

#ip_hash 不支持backup等
upstream code{
          ip_hash;
          server 192.168.0.250:8081 ;
          server 192.168.0.250:8082 ;
          server 192.168.0.251;
         # server 192.168.0.250:8083 backup;
        }

url_hash
Syntax:hash key [consistent];
Default:--
Context:upstream;
This directive appeared in version 1.7.2

upstream code{
          hash $request_uri;
          server 192.168.0.250:8081 ;
          server 192.168.0.250:8082 ;
          server 192.168.0.251;
         
        }

        
        server {
                listen       80;
                server_name    192.168.0.250 ;
                #root    /mnt/hgfs/www/web/thread/process_thread_study/swoole;

                access_log  logs/host.access.log  main;

                location / {
                        proxy_pass http://code;
                }
        }

        include ./hosts/*.conf;

同一url访问到的机器是一致的

http://192.168.0.250/ 访问到的始终是同一台 比如 192.168.0.251
http://192.168.0.250/index1.php 访问到的始终是同一台 比如 192.168.0.250:8081

 

keepalive
保持活动连接 ,nginx服务器将会为每一个worker进程保持同上游服务器的连接
连接缓存在nginx需要同上游服务器持续保持一定数量的连接时非常有用
如果上游服务器通过http进行对话,那么nginx将会使用http/1.1协议的持久连接机制维护这些打开的连接
upstream apache {
server 127.0.0.1:8080;
keepalive 32;
}
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://apache;
}
初始的时候nginx仅需为每个worker打开32个tcp连接,然后通过不发送close的connection头保持这些连接的打开

 

FastCGI上游服务器
upstream fastcgis{
  server 10.88.1.10:9000;
  server 10.88.1.20:9000;
  server 10.88.1.30:9000;
}
location / {
  fastcgi_pass fastcgis;
}

 

posted @ 2018-07-07 22:52  H&K  阅读(535)  评论(0编辑  收藏  举报