NGINX负载均衡配置详解 (二)

前言

nginx负载均衡是nginx服务中常用的功能,当单台服务器不能满足用户请求时,为了避免服务器崩溃,让用户有个好的体验,需要用多台服务器集群做nginx反向代理,并且根据策略分担服务器压力。

一、实验环境

node1 192.168.1.121 代理服务器
node2 192.168.1.122 后端服务器
node3 192.168.1.123 后端服务器

二、环境部署

1.源码包安装

源码安装详解←戳这里

三台服务器同样安装nginx服务

[root@node1 nginx-1.18.0]# yum install pcre-devel zlib-devel -y #安装依赖
[root@node1 src]# tar xf nginx-1.18.0.tar.gz 
[root@node1 src]# cd nginx-1.18.0/
[root@node1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
[root@node1 nginx-1.18.0]# make && make install

2.设置别名

设置临时别名

[root@node1 nginx-1.18.0]# alias nginx=/usr/local/nginx/sbin/nginx

3.部署代理服务器

反向代理语法
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
反向代理配置
在http指令块下配置upstream指令块
upstream web-server{ 
           server 192.168.1.122;  #后端服务器
           server 192.168.1.123;  #后端服务器
   }
   
在location指令块配置proxy_pass
location / {
            proxy_pass http://web-server;  #定义群组名 
            root   html;   
            index  index.html index.htm;
        }
[root@node1 nginx-1.18.0]# nginx #启动服务

4.部署后端服务器

配置后端服务器
安装nginx服务
1.122服务器配置默认访问页面
[root@node2 nginx-1.18.0]# echo "this is 1.122 page " > /usr/local/nginx/html/index.html
[root@node2 nginx-1.18.0]# /usr/local/nginx/sbin/nginx
[root@node2 nginx-1.18.0]# curl 127.0.0.1
this is 1.122 page 
安装nginx服务
1.123服务器配置默认访问页面
[root@node3 nginx-1.18.0]# echo "this is 1.123 page " > /usr/local/nginx/html/index.html
[root@node3 nginx-1.18.0]# /usr/local/nginx/sbin/nginx
[root@node3 nginx-1.18.0]# curl 127.0.0.1
this is 1.123 page 

5.负载均衡方式-七层

四层均衡←戳这里
轮询
可以看到后端服务器,非常平均的处理请求
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.123 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.123 page 
轮询加权重

修改配置文件

upstream web-server{
           server 192.168.1.122 weight=3;  #weight 后端服务根据权重比例处理请求,适用于服务器性能不均的环境
           server 192.168.1.123 weight=1;
   }
[root@node1 nginx-1.18.0]# /usr/local/nginx/sbin/nginx -s reload
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.123 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.123 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121
this is 1.123 page 
ip hash
upstream web-server{
                ip_hash;  #通过客户端ip进行hash,同一个ip的请求会指定到同一个后端服务器服务器
           server 192.168.1.122 weight=3;
           server 192.168.1.123 weight=1;
   }
[root@node1 nginx-1.18.0]# !/usr
/usr/local/nginx/sbin/nginx -s reload
[root@node1 nginx-1.18.0]# while true;do curl 192.168.1.121;sleep 2;done
this is 1.122 page  #请求都被1.122后端处理
this is 1.122 page 
this is 1.122 page 
this is 1.122 page 
^C
[root@node1 nginx-1.18.0]# curl 192.168.1.123  #1.123服务器正常
this is 1.123 page 
url_hash
通过请求url进行hash,再通过hash值选择后端server
upstream web-server{
                hash $request_uri consistent;
           server 192.168.1.122 weight=3;
           server 192.168.1.123 weight=1;
   }

[root@node1 nginx-1.18.0]# !/usr
/usr/local/nginx/sbin/nginx -s reload
[root@node1 nginx-1.18.0]# curl 192.168.1.121/index.html
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121/index.html
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121/index.html
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121/index.html
this is 1.122 page 
[root@node1 nginx-1.18.0]# curl 192.168.1.121/index.html
this is 1.122 page 

最大错误连接数
upstream web-server{
           server 192.168.1.122 weight=1 max_fails=3 fail_timeout=5s; 
           server 192.168.1.123 weight=1;
   }
location / {
            proxy_pass http://web-server;
            proxy_next_upstream off; #关闭这个,不然看不到效果 不写默认为error
            root   html;
            index  index.html index.htm;
        }

[root@node1 nginx-1.18.0]# !/usr
/usr/local/nginx/sbin/nginx -s reload
[root@node1 nginx-1.18.0]# while true;do curl 192.168.1.121 ;sleep 1; done  #正常访问
this is 1.122 page 
this is 1.123 page 
this is 1.122 page 
this is 1.123 page 
this is 1.122 page 

[root@node2 nginx-1.18.0]# /usr/local/nginx/sbin/nginx -s stop #把后端服务器1.122 -s stop
[root@node1 nginx-1.18.0]# while true;do curl 192.168.1.121 ;sleep 1; done #后端挂了一个服务器
<!DOCTYPE html> #这是错误 1次
<html>
<head>
<title>Error</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>

this is 1.123 page  #正常访问

<!DOCTYPE html>  #这是错误 2次
<html>
<head>
<title>Error</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>

this is 1.123 page   #正常访问

<!DOCTYPE html> #这是错误  3次
<html>
<head>
<title>Error</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>

this is 1.123 page  #正常访问
this is 1.123 page 
this is 1.123 page 
this is 1.123 page 
this is 1.123 page 
this is 1.123 page
 
<!DOCTYPE html>  #这是重试一次
<html>
<head>
<title>Error</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>
this is 1.123 page  #剔除故障服务器正常访问
this is 1.123 page 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦读

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值