Nginx配置负载均衡

在上一章我们讲到Nginx反向代理的配置,这章我们接着后面继续,没看到的小伙伴可以坐一下时光机;

时光机➡️:Nginx的反向代理-CSDN博客

一、配置Nginx负载均衡

1)安装其他两台虚拟机的Nginx服务;

bash 复制代码
# 这两台直接使用傻瓜安装,这次我们要注意的是Nginx的文件的路径
# nginx.conf = /etc/nginx/nginx.conf 
# html = /usr/share/nginx/html

# 直接使用yum工具安装
yum install -y nginx

2)配置两台虚拟机的HTML文件(为了方便后面查看负载均衡);

bash 复制代码
# 172.16.52.102 

vim /usr/share/nginx/html

Hello,My ip is 172.16.52.102

# 172.16.52.103

vim /usr/share/nginx/html

Hello,My ip is 172.16.52.103

3)启动Nginx;

bash 复制代码
# 使用yum工具安装的本身就是system级别的,在bin目录下是有启动文件的,所以我们可以直接使用系统启动
# 启动Nginx

systemctl start nginx 

# 扩展:
systemctl stop nginx # 停止
systemctl reload nginx # 重载

4)查看是否启动成功;

5)配置负载均衡(配置101这台服务器,其他不需要配置);

bash 复制代码
# 回到172.16.52.101这台服务器上配置nginx.conf文件

vim /usr/nginx/conf/

worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;

    keepalive_timeout  65;

    upstream nginxtest {                 
	server 172.16.52.102:80 weight=1;
	server 172.16.52.103:80 weight=1;
       }

    server {
        listen       80;
        server_name  localhost;

        location / {
           proxy_pass http://nginxtest;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

6)配置文件讲解;

bash 复制代码
# 其中轮询是指客户访问服务,被指定到其中一台服务的次数,都是1说明一个服务轮一次,如果是2和1,意思是3次访问中一台是2次,一台是1次;

7)测试;

第一次访问

第二次访问

第三次访问

以上结果说明我们的负载均衡配置成功,并且轮询也成功!

相关推荐
ZeroNews内网穿透14 小时前
新版发布!“零讯”微信小程序版本更新
运维·服务器·网络·python·安全·微信小程序·小程序
<但凡.14 小时前
Linux 修炼:进程控制(一)
linux·运维·服务器·bash
✎﹏赤子·墨筱晗♪15 小时前
Ansible Playbook 入门指南:从基础到实战
linux·服务器·ansible
tritone1 天前
我在阿贝云免费服务器上搭建RustDesk自建服务器(Self-Hosting)的真实体验【推荐】
运维·服务器
洲覆1 天前
Redis 核心数据类型:从命令、结构到实战应用
服务器·数据库·redis·缓存
岁岁种桃花儿1 天前
详解 Kubernetes 命令:kubectl exec -it nginx -- bash 及实战场景
运维·nginx·kubernetes
小牛马爱写博客1 天前
DNS 服务器与 DHCP 服务器详解及配置指南
linux·运维·服务器·dns·dhcp
维尔切1 天前
HAProxy 负载均衡器
linux·运维·数据库·负载均衡
什么半岛铁盒1 天前
C++项目:仿muduo库高并发服务器-------Channel模块实现
linux·服务器·数据库·c++·mysql·ubuntu
2503_924806851 天前
动态IP使用中 报错407 怎么办???
服务器·tcp/ip·php