nginx之负载均衡应用

一、负载均衡作用:缓解用户访问网站服务器的压力
环境准备:

主机名IP地址部署软件
lb0110.0.0.5nginx
web0110.0.0.7nginx
web0210.0.0.8nginx
web0310.0.0.9nginx

二、 web服务器网站文件配置信息

[root@web1 ~]# cat /etc/nginx/conf.d/mmm.conf
server  {
        listen      80;
        server_name www.newboy.com;
        location /  {
        root  /html/mmm/;
        index  index.html;
        }
}

[root@web2 ~]# cat /etc/nginx/conf.d/mmm.conf
server  {
        listen      80;
        server_name www.newboy.com;
        location /  {
        root  /html/mmm/;
        index  index.html;
        }
}

[root@web3 ~]# cat /etc/nginx/conf.d/mmm.conf 
server  {
        listen      80;
        server_name www.newboy.com;
        location /  {
        root  /html/mmm/;
        index  index.html;
        }
}

三、web服务器存放网站文件目录

[root@web1 ~]# cat /html/mmm/index.html 
web-01
[root@web2 ~]# cat /html/mmm/index.html 
web-02
[root@web3 ~]# cat /html/mmm/index.html 
web-03

四、web服务器/etc/nginx/nginx.conf文件配置

[root@web1 ~]# cat /etc/nginx/nginx.conf 
#把没用的注释文件删掉之后
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/mmm.conf;  #这里修改为网站配置文件的具体文件名
}
[root@web1 ~]# scp /etc/nginx/nginx.conf 10.0.0.8:/etc/nginx/
[root@web1 ~]# scp /etc/nginx/nginx.conf 10.0.0.9:/etc/nginx/

五、重启 nginx服务
systemctl restart nginx
六、利用负载均衡服务器测试web页面是否能够访问

[root@lb1 ~]# curl -H host:www.newboy.com 10.0.0.7
web-01
[root@lb1 ~]# curl -H host:www.newboy.com 10.0.0.8
web-02
[root@lb1 ~]# curl -H host:www.newboy.com 10.0.0.9
web-03

七、编写负载均衡服务器配置文件信息

[root@lb1 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    include             /etc/nginx/mime.types;
upstream oldboy {             #定义主机组名称
        server 10.0.0.7:80;   #web节点
        server 10.0.0.8:80;
        server 10.0.0.9:80;
}
      server {                #网站配置文件
             listen  80;
             server_name www.newboy.com;
      location  /  {
             proxy_pass http://oldboy; #调用主机组
    }
}
}

八、重启nginx服务
systemctl restart nginx
九、配置host解析
10.0.0.5 www.newboy.com
十、网页测试访问,刷新页面
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值