nginx 负载均衡

upstream 配置php

在http模块
upstream app {html

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

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

}算法

server配置参数apache

 

指令后端

含义缓存

down服务器

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

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;
}

相关文章
相关标签/搜索