用虚拟机实现nginx七层的负载均衡

用虚拟机实现nginx七层的负载均衡

1.准备三台安装好nginx服务的虚拟机。

#由于是演示环境下,可以在后端服务器创建临时IP

ip addr add 192.168.73.11/24 dev ens36 #添加临时网卡。
ip addr del 192.168.73.11/24 dev ens36 #删除临时网卡。
#添加临时网卡时网段必须相同!!!!!

2.配置nginx代理端


[root@proxy conf.d]# cat proxy.conf 
upstream index {
    
    
    server 192.168.73.11:80 weight=1 max_fails=2 fail_timeout=2;
    server 192.168.73.12:80 weight=2 max_fails=2 fail_timeout=2;
   }
upstream news {
    
    
    server 192.168.73.21:80 weight=1 max_fails=2 fail_timeout=2;
    server 192.168.73.22:80 weight=2 max_fails=2 fail_timeout=2;
   }

upstream milis {
    
    
    server 192.168.73.31:80 weight=1 max_fails=2 fail_timeout=2;
    server 192.168.73.32:80 weight=2 max_fails=2 fail_timeout=2;
  }
upstream videos {
    
    
    server 192.168.73.41:80 weight=1 max_fails=2 fail_timeout=2;
    server 192.168.73.42:80 weight=2 max_fails=2 fail_timeout=2;
   }

upstream images {
    
    
    server 192.168.73.51:80 weight=1 max_fails=2 fail_timeout=2;
    server 192.168.73.52:80 weight=2 max_fails=2 fail_timeout=2;
   }

server {
    
    
       location / {
    
    
     proxy_pass http://index/;
     }
     
     location  /news {
    
    
     proxy_pass http://news/;
     }
     
     location /milis {
    
    
     proxy_pass http://milis/;
     }
     
     location /videos {
    
    
     proxy_pass http://videos/;
     }
     
     location /images {
    
    
     proxy_pass http://images/;
     }
}

3.配置后端服务器

1.创建对应代理端的配置文件

!!!这里指展示了web1服务器,web2服务器也要做同样的配置
[root@web1 nginx]# tree 
#在 /etc/nginx/conf.d/下创建
.
├── conf.d
│   ├── images.conf
│   ├── index.conf
│   ├── milis.conf
│   ├── news.conf
│   └── videos.conf

#下面是一个固定写法,添加到上面的配置文件需要做修改。

server {
    
    
    listen       192.168.73.11:80;  			#对应的IP代理端的访问ip地址
    server_name  www.index.com;     			#设置域名解析的可以进
    root         /usr/share/nginx/html; 	#访问目录
    access_log   /var/log/nginx/index-access.log  main; #日志文件存放位置

    # Load configuration files for the default server block.
    # include /etc/nginx/default.d/*.conf;
    location /  {
    
    
        index index.html  index.hml;  
    }

    error_page 404 /404.html;
    location = /404.html {
    
    
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    
    
    }
}    

2.创建对应代理端的访问目录和index.html

!!!这里指展示了web1服务器,web2服务器也要做同样的配置
[root@web1 html]# tree
#在 /usr/share/nginx/html下创建对应目录和index.html;
#可以在index.html上添加不同的内容用来测试负载均衡是否成功;
.
├── 404.html
├── 50x.html
├── en-US -> ../../doc/HTML/en-US
├── icons
│   └── poweredby.png -> ../../../pixmaps/poweredby.png
├── images
│   └── index.html
├── img -> ../../doc/HTML/img
├── index.html -> ../../doc/HTML/index.html
├── index.php
├── milis
│   └── index.html
├── news
│   └── index.html
├── nginx-logo.png
├── poweredby.png -> nginx-logo.png
├── videos
│   └── index.html
└── web1
    └── index.html

4.测试负载均衡

[root@proxy ~]# curl 192.168.73.128/news
news2
[root@proxy ~]# curl 192.168.73.128/news
news1
[root@proxy ~]# curl 192.168.73.128/milis
milis2
[root@proxy ~]# curl 192.168.73.128/milis
milis1
[root@proxy ~]# curl 192.168.73.128/videos
videos2
[root@proxy ~]# curl 192.168.73.128/videos
videos1
[root@proxy ~]# curl 192.168.73.128/images
images2
[root@proxy ~]# curl 192.168.73.128/images
images1
#这里可以看到可以成功的访问到后端服务器。

猜你喜欢

转载自blog.csdn.net/qq1271566323/article/details/142365210