Nginx负载均衡配置实战详解

1、实验环境

Nginx 192.168.1.101 80

tomcat01 192.168.1.102 8080

tomcat02 192.168.1.103 8080

2、负载均衡主流算法

轮询,权重,IP哈希算法,最少连接数,fair(第三方插件)

3、轮询

配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
 #轮询
    upstream web {                                          
            server 192.168.1.102:8080;
            server 192.168.1.103:8080;
}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            proxy_pass http://web;                   #此处配置地址为上面的自定义名称web
            index  index.html index.htm;
        }
}

 测试配置

192.168.1.102

vi /usr/local/tomcat9/webapps/ROOT/index.html
配置内容如下
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>淘淘网</title>
</head>
<body>
淘淘网001号服务器
</body>
</html>

 192.168.1.103

vi /usr/local/tomcat9/webapps/ROOT/index.html
配置内容如下
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>淘淘网</title>
</head>
<body>
淘淘网002号服务器
</body>
</html>

效果

备注:如果只轮询一台,请备份tomcat安装文件里的webapps下ROOT目录,然后删除ROOT目录下所有文件只保留index.html文件

4、权重

192.168.1.102为五分之四

192.168.1.103为五分之一

配置nginx.conf
vi //usr/local/nginx/conf/nginx.conf
配置内容如下
 #权重
    upstream web {
         server 192.168.1.102:8080 weight=4;
         server 192.168.1.103:8080;
}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            proxy_pass http://web;
            index  index.html index.htm;
        }
}

5、哈希算法

请求按访问ip的hash结果分配,这样每个IP固定访问一 个应用服务器,可以解决session共享的问题,配置如下

因为访问者的IP地址是固定的,那么根据IP地址的Hash值也是感光固定的,那 么一个ip就一直访问对应的那个服务器,不会跨服务器

配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
 #权重
    upstream web {
         ip_hash;
         server 192.168.1.102:8080;
         server 192.168.1.103:8080;
}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            proxy_pass http://web;
            index  index.html index.htm;
        }
}

6、最少连接数

配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
 #权重
    upstream web {
         least_conn;
         server 192.168.1.102:8080;
         server 192.168.1.103:8080;
}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            proxy_pass http://web;
            index  index.html index.htm;
        }
}

7、fair(第三方插件)

配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
    upstream web {
         server 192.168.1.102:8080;
         server 192.168.1.103:8080;
         fair;
}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            proxy_pass http://web;
            index  index.html index.htm;
        }
}

8、负载均衡备机和宕机配置

备机backup配置
当其他非backup机器挂掉后,才会请求backup机器

配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
    upstream web {
         server 192.168.1.102:8080;
         server 192.168.1.103:8080 backup;
}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            proxy_pass http://web;
            index  index.html index.htm;
        }
}

 

宕机down配置
配置down的服务器不参与负载均衡,这样down所标记的服务器就可以安心的升级了

配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
    upstream web {
         server 192.168.1.102:8080;
         server 192.168.1.103:8080 down;
}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            proxy_pass http://web;
            index  index.html index.htm;
        }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值