【Web集群】Nginx 负载均衡实战

硬件设备

三台虚拟机

 

Roel

HOSTNAME

IP

说明

Proxy host

Node 1

192.168.74.153

Nginx代理主机

Web Server 1

Node 2

192.168.74.162

Web服务器1

Web Server 2

Node 3

192.168.74.163

Web服务器2

 

软件设备

软件准备      系统:CentOS7.x x86_64

软件:nginx-1.16.0-1.el7.ngx.x86_64.rpm

配置

1、配置虚拟主机(两台web服务器配置)

[root@node2 ~]# vim /etc/nginx/conf.d/vhost.conf

server {
        listen       80;
        server_name  bbs.yunjisuan.com;

        location / {
            root  /usr/share/nginx/html/bbs;
            index  index.html index.htm;
        }
}

    server {
        listen       80;
        server_name  blog.yunjisuan.com;

        location / {
            root   /usr/share/nginx/html/blog;
            index  index.html index.htm;
        }
}

2、准备web测试界面(两台web服务器配置)

[root@node2 ~]# mkdir -p /usr/share/nginx/html/{blog,bbs}/logs
[root@node2 ~]# echo "`hostname -I `blog test page" > /usr/share/nginx/html/blog/index.html
[root@node2 ~]# echo "`hostname -I `bbs test page" > /usr/share/nginx/html/bbs/index.html

3、启动Nginx服务(两台web服务器配置)

[root@node2 ~]# systemctl start nginx.service 
[root@node2 ~]# systemctl enable nginx.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

4、配置负载均衡(代理主机配置)

[root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf

    upstream bbs_pool {
        server 192.168.74.162;
        server 192.168.74.163;
        }
 
    upstream blog_pool {
        server 192.168.74.162;
        server 192.168.74.163;
        }

    server {
        listen 80;
        server_name bbs.yunjisuan.com;
        
        location / {
            proxy_pass http://bbs_pool;
        }
    }
        
    server {
        listen 80;
        server_name blog.yunjisuan.com;
        
        location / {
            proxy_pass http://blog_pool;
        }
    }

5、重启Nginx服务

[root@node01 ~]# systemctl start nginx

6、修改测试主机本地hosts文件

[root@node1 ~]# vim /etc/hosts
192.168.74.153 bbs.yunjisuan.com blog.yunjisuan.com

6、测试

[root@localhost ~]# for i in `seq 6`; do curl http://blog.yunjisuan.com; done
192.168.74.162 bbs test page
192.168.74.163 bbs test page
192.168.74.162 bbs test page
192.168.74.163 bbs test page
192.168.74.162 bbs test page
192.168.74.163 bbs test page

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值