nginx的负载均衡模块详解

反向代理与负载均衡的概念:

    Nginx仅仅是作为nginx proxy反向代理使用,nginx其实是反向代理,只不过是有负载均衡的功能!

安装Nginx负载均衡

官网Nginx模块讲解

模块详解

upstream模块

upstream www {         #upstream是关键字,必须要写,后面的www是一个群组名字,自己起名
server 192.168.70.127:80 weight=1;   #  server固定关键字,后面可以接域名或者IP,如果不指定端口默认80.

                                                             #weight权重,数值越大被分配的请求越多,结尾有分号
server 192.168.70.126:80 weight=1 bakup;#bakcup相当于热备

server 192.168.79.128:80 weight=1 max_fails=2 fail_timeout=20s;#max_fails尝试连接后端主机失败的次数,这个值配合 proxy_next_upstream、                                                                                                                fastcgi_next_upstream和memcached_next_upstream这三个参数来使用。根据需求配置,不要太大建议1-3次

                                                                                     fail_timeout=20s 失败秒数,在达到max_fails尝试连接失败次数后,休息20秒,在次连接默认10秒,建议2-3秒

}

upstream模块调度算法

调度算法一份分为两类:

  第一类  静态调度算法

        分配的时候,不需要考虑后端节点服务器的情况(rr, wrr, ip_hash调度算法)

      第二类   动态调度算法

        根据自身的情况调度,根据后端节点状态是否响应很快进行调度(least_conn,fair等)

静态调度算法

  rr轮询(默认调度算法)

                   按照客户端请求顺序把客户端的请求逐一分配到不同后端节点服务器

    wrr权重轮序

                 在rr轮询算法的基础上加上权重,权重值越大,别转发的请求越多,可以根据服务器状态进行指定权重值大小

      ip_hash

                 每个请求按照客户端IP的hash结果分配,新的请求到达时,先将客户端IP通过哈希算法哈希出一个值,在随后的的客户端请求中,客户IP的哈希值只要相同,就会被分配至同一台服务器,该调度算法可以解决动态网页的session共享问题,但是会导致请求分配不均

      (在upstream里配置,如果是ip_hash,不能有weight和bakcup)

    

        upstream www{

                      ip_hash:

                            server xxxx

                         }

动态调度算法

fair

   fair算法会根据后端节点服务器的响应时间来分配请求,时间短的优先分配,必须下载nginx的相关模块upstream_fair

  upstream www{

 fair;

  server xxx;

 server xxx;

}

least_conn

   least_conn算法根据后端节点的连接数来决定分配情况,哪个机器连接数少就分发

url_hash算法(web cache)

   和ip_hash类似,一般用于web缓存,根据访问URL的hash结果来分配请求的,每个URL定向到同一个后端服务器,后端服务器为缓存服务器时效果明显。

upstream www{

server xxxx

hash $request_uri;

hash_method crc32;

}

一致性hash算法

    一致性hash算法一般用于代理后端业务为缓存服务(squid,memcached)的场景,通过将用户请求的URI或指定字符串进行计算,然后调度到后端服务器上,此后任何用户查找同一个RUI或者指定字符串都会被调度到这一台服务器上,此后后端的每个节点缓存的内容都是不同的。

http{

  upstream www{

     consistent_hash $request_uri;

      server xxxx id=1001  weigh=3;

 }

}

http_proxy_module模块

proxy_pass转发代理

    location /name/ {

               proxy_pass http://127.0.0.1/remote/;

}

相关参数

  • proxy_set_header     设置http请求header项传给后端服务器节点,可实现让代理后端服务器节点获取访问客户端用户真实IP地址 proxy_set_header Host  $host;
  • client_body_buffer_size  用于指定客户端请求主体缓冲区大小
  • proxy_connect_timeout   表示反向代理与后端节点服务器连接的超时时间
  • proxy_send_timeout        代理后端服务器的数据回传时间,在规定时间之内服务器必须传完所有数据,否则断开
  • proxy_read_timeout         设置nginx从代理的后端服务器获取信息时间,表示连接建立成功后,nginx等待后端服务器的响应时间
  • proxy_buffer_size     设置缓冲区大小,默认该缓冲区大小等于指令proxy_buffers设置大小
  • proxy_buffers              这是缓冲区的数量和大小,nginx从代理的后端服务器获取响应信息
  • proxy_busy_bufers_size  用于设置系统很忙时可以使用的proxy_buffers大小,官方推荐proxy_buffer*2
  • proxy_temp_file_write_size  临时缓存文件

 反向代理重要参数

prox_pass http://server_pools;      通过proxy_pass功能把用户的请求转向到反向代理定义的upstream服务器

proxy_set_header Host $host;     在代理向后端服务器发送的http请求头中加入host字段信息,用于后端服务器配置有多个虚拟主机,可以识别那个虚拟主机

proxy_set_header X-Forwarded-For $remot_addr;  用于接收用户真实IP,而不是代理服务器ip

在配置文件里都会加上include proxy.conf;

在proxy.conf里增加参数,会显得干净

proxy_set_header Host  $host;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_connect_timeout 60;

proxy_send_timeout 60;

proxy_read_timeout 60;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

=========================NGINX--根据URL中的目录地址实现代理转发====================

当用户请求www.daxian.com/upload/xx地址的时候,实现由upload上传服务器池处理请求

当用户请求www.daxian.com/static/xx地址的时候,实现由静态服务器池处理请求

除此之外,对于其他访问请求,全部交给默认动态服务器请求

环境 131负载均衡   126nginx  127apache(服务器有限,127当两台服务器)

配置过程

在131nginx配置文件中添加

   upstream static_pools {
        server 192.168.70.127:80 weight=1;#apache
    }
   upstream upload_pools{
        server 192.168.70.127:8080 weight=1;
    }
   upstream default_pools{
        server 192.168.70.126:80 weight=1;#nginx
   }

server {
listen 80;
server_name www.daxian.com;
location / {
proxy_pass http://default_pools;
include proxy.conf;
}


location /static/ {
proxy_pass http://static_pools;
include proxy.conf;
}


location /upload/ {
proxy_pass http://upload_pools;
include proxy.conf;
}


}

 

配置apache

cd /application/apache/conf/extra/

vim httpd-vhosts.conf 

<VirtualHost *:80>
ServerAdmin 2647@qq.com
DocumentRoot "/application/apache2.2.34/htdocs/www"
ServerName www.daxian.com
ServerAlias daxian.com
ErrorLog "logs/www-error_log"
CustomLog "logs/www-access_log" common
</VirtualHost>


<VirtualHost *:8080>
ServerAdmin 2647@qq.com
DocumentRoot "/application/apache2.2.34/htdocs/www8080"
ServerName www.daxian.com
ErrorLog "logs/www8080-error_log"
CustomLog "logs/www8080-access_log" common
</VirtualHost>

 
增加监听端口8080
vim /application/apache/conf/httpd.conf

Listen 80
Listen 8080

创建访问目录 mkdir
-p /application/apache/htdocs/www8080 echo www8080>/application/apache/htdocs/www8080/index.html cd /application/apache/htdocs/www/ mkdir static/ echo static>index.html ../../bin/apachectl restart

web01修改hosts

192.168.70.127 web02  www.daxian.com

进行测试

curl http://www.daxian.com
apachewww

curl http://www.daxian.com/static/
static

cd www8080

mkdir /upload/

cd /upload/

echo "upload">index.html

测试

curl http://www.daxian.com:8080/upload/
upload

验证结果

web01 修改hosts文件,将地址指向负载均衡器

192.168.70.131 lb01 www.daxian.com

ping www.daxian.com
PING lb-01 (192.168.70.131) 56(84) bytes of data.
64 bytes from lb-01 (192.168.70.131): icmp_seq=1 ttl=64 time=0.571 ms
64 bytes from lb-01 (192.168.70.131): icmp_seq=2 ttl=64 time=0.686 ms
64 bytes from lb-01 (192.168.70.131): icmp_seq=3 ttl=64 time=0.685 ms

测试

curl http://www.daxian.com
nginx wwww

curl http://www.daxian.com/static/
static

curl http://www.daxian.com/upload/
upload

 ============================nginx根据移动端转发============================

在7层负载均衡下不需要人为拆分域名,对外只需要用一个域名即可,通过获取用户请求中设备信息($http_user_agent获取)

vim nginx.conf

worker_processes 1;

events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream android_pools {
server 192.168.70.127:80 weight=1; #apache
}
upstream iphone_pools {
server 192.168.70.127:8080 weight=1;

}
upstream pc_pools {
server 192.168.70.126:80 weight=1; #nginx
}
server {
listen 80;
server_name www.daxian.com;

location / {

if ($http_user_agent ~* "android")
{
proxy_pass http://android_pools;
}
if ($http_user_agent ~* "iphone")
{
proxy_pass http://iphone_pools;
}
proxy_pass http://pc_pools;
include proxy.conf;
}
}
}

../sbin/nginx -s reload

局域网里就可以输入ip地址访问

安卓

http://192.168.70.131/static/

苹果

http://192.168.70.131/upload/

pc

http://192.168.70.131

原文地址:https://www.cnblogs.com/sky00747/p/8628866.html