1

Nginx的负载均衡实现,你学会了吗?

 6 months ago
source link: https://server.51cto.com/article/775852.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

192.168.50.60

nginx-1

httpd

192.168.50.61

nginx-2

httpd

proxy

192.168.50.62

负载均衡器

nginx

负载均衡策略

nginx的负载均衡用于upstream模板定义的后端服务器列表中选取一台服务器接收用户的请求。一个基本的upstream模块如下:

upstream [服务器组名称]{
  server [IP地址]:[端口号];
  server [IP地址]:[端口号];
  ....
}

在upstream模块配置完成后,要让指定的访问反向代理到服务器列表,格式如下:

location ~ .*$ {
  index index.jsp index.html;
  proxy_pass http://[服务器组名称];
}

这样就完成了最基本的负载均衡,但是这并不能满足实际需求。目前Nginx的upstream模块支持6种方式的负载均衡策略(算法):

最基本的配置方法,是upstream模块默认的负载均衡策略。每个请求会按时间顺序平均分配到不同的后端服务器。有如下参数:

fail_timeout与max_fails结合使用
max_fails在fail_timeout参数设置的时间内最大失败次数。如果在这个时间内,所有该服务器的请求都失败了,那么认为该服务器停机
fail_time服务器被认为停机的时长,默认10s(被认为停机的服务器尝试间隔?)
backup标记该服务器为备用服务器。当主服务器停止时,请求会被发送到它这里
down标记服务器永久停机

注意:1.down标记的服务器会自动剔除;2.缺省就是轮询;3.此策略适合服务器配置无状态且短平块的服务使用

weight

权重方式,在轮询策略的基础上指定轮询的几率。也可以认为是在轮询的基础上新增了一个weight的参数,此参数指定轮询的几率,值为number。upstream模块配置模板如下:

upstream [服务器组名称]{
  server [IP地址]:[端口号] weight=2;
  server [IP地址]:[端口号];
  ....
}

在该例子中,没有weight参数的服务器默认为1,weight的数值与访问比例成正比,所有weight值的总和为一个循环单位,服务器自身的weight值为循环单位内的轮询次数。

注意:1.权重越高分配到的请求越多;  2.此策略可以和least_conn策略、iphash策略结合使用;  3.此策略比较适合服务器硬件配置差距较大的情况。

ip_hash

依据ip分配方式,指定负载均衡器按照基于客户端IP的分配方式,这个方法确保了相同的客户端请求一致发送到相同的服务器,以保证session会话。这样每个访客都固定访问一个后端服务器,可以解决session不能跨服务器的问题。upstream模块配置模板如下:

upstream [服务器组名称]{
  ip_hash;
  server [IP地址]:[端口号] weight=2;
  server [IP地址]:[端口号];
  ....
}

注意:1.nginx1.3.1之前的版本不能在ip_hash中使用权重(weight);2..ip_hash不能与backup同时使用;3.此策略适合有状态服务的程序,比如session;4.当有服务器需要剔除,必须手动down掉。

least_conn

最少连接方式,把请求发给链接数最少的后端服务器。轮询是把请求平均分配给各个后端,使它们的负载大致相同。但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,least_conn这种方式就可以达到更好的负载均衡效果。upstream模块配置模板如下:

upstream [服务器组名称]{
  least_conn;
  server [IP地址]:[端口号] weight=2;
  server [IP地址]:[端口号];
  ....
}

注意:此策略适合请求处理时间长短不一造成的服务器过载情况。

响应时间方式,按照服务器端的响应时间来分配请求,响应时间短的优先分配。upstream模块配置模板如下:

upstream [服务器组名称]{
  server [IP地址]:[端口号] weight=2;
  server [IP地址]:[端口号];
  ....
  fair;
}

注意:需要安装第三方插件。

url_hash

url分配方式,按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用。同一个资源多次请求可能会到达不同的服务器上,导致不必要的多次下载,缓存命中率不高,以及一些资源时间的浪费。而使用url_hash,可以使得同一个url(也就是同一个资源请求)会到达同一台服务器,一旦缓存住了资源,再次收到请求,就可以在缓存中读取。upstream模块配置模板如下:

upstream [服务器组名称]{
  hash $request_uri;
  server [IP地址]:[端口号] weight=2;
  server [IP地址]:[端口号];
  ....
}

注意:1.需要安装第三方插件;2.uri,是i,不是小写的L。

安装proxy

proxy 机器 编译安装nginx

工具包安装

wget http://nginx.org/download/nginx-1.14.2.tar.gz
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
tar xf nginx-1.14.1.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.14.1/
./configure --prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module
make && make install
echo $?

参数说明:

--with-http_dav_module,启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启;
    --with-http_stub_status_module,启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态);
    --with-http_addition_module,启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求);
    --with-http_sub_module,启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本);
    --with-http_flv_module,启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件);
    --with-http_mp4_module,启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件)。

生成nginx 用户

useradd -M -s /sbin/nologin nginx

启动和自启

/usr/local/nginx/sbin/nginx
echo /usr/local/nginx/sbin/nginx >> /etc/rc.local
chmod +x /etc/rc.d/rc.local

如果防火墙是开启的记得添加端口号firewall-cmd --permanent --zone=public --add-port=80/tcp && firewall-cmd --reload

conf 文件修改

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
  worker_connections 1024;
}
http {
  include       mime.types;
  default_type application/octet-stream;
  sendfile       on;
  keepalive_timeout 65;
  server_tokens off;

upstream htmlservers {
server 192.168.50.60:80 weight=1;
server 192.168.50.61:80 weight=1;
}
upstream phpservers{
server 192.168.50.60:80 weight=1;
server 192.168.50.61:80 weight=1;
}
upstream picservers {
server 192.168.50.60:80 weight=1;
server 192.168.50.61:80 weight=1;
}
include vhosts/*.conf;
}

vim /usr/local/nginx/conf/vhosts/upstream.conf

server {
listen       80;
server_name localhost;
location / {
if ($request_uri ~* \.html$){
  proxy_pass http://htmlservers;
}
if ($request_uri ~* \.php$){
proxy_pass http://phpservers;
}
proxy_pass http://picservers;
}
}

web服务器安装

yum install httpd -y

设置主页文件并启动

echo web1 > /var/www/html/index.htmlecho web2 > /var/www/html/index.html echo php1 > /var/www/html/index.phpecho php2 > /var/www/html/index.phpecho jsp1 > /var/www/html/index.jspecho jsp2 > /var/www/html/index.jspsystemctl start httpd

如果防火墙是开启的,记得添加端口号firewall-cmd --permanent --zone=public --add-port=80/tcp && firewall-cmd --reload firewall-cmd --permanent --zone=public --add-port=80/tcp && firewall-cmd --reload


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK