首页 > 系统相关 >##Nginx反向代理负载均衡

##Nginx反向代理负载均衡

时间:2023-03-07 16:01:56浏览次数:32  
标签:负载 nginx ## etc Nginx systemctl yum && root

介绍: Ngin x是一个高性能的http和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务,特点:占有内存少,并发能力强。作为中间件具有如下功能:1、制作静态页面 2、反向代理 3、负载均衡 4、动静分离 5、会话保持

 端口号:80

主配置文件:vim /usr/local/nginx/conf/nginx.conf

负载均衡的三种方式

负载均衡有三种部署方式:路由模式、桥接模式、服务直接返回模式。路由模式部署灵活,约60%的用户采用这种方式部署;桥接模式不改变现有的网络架构;服务直接返回(DSR)比较适合吞吐量大特别是内容分发的网络应用。约30%的用户采用这种模式。

1、路由模式(推荐)

路由模式的部署方式,服务器的网关必须设置成负载均衡机的LAN口地址,且与WAN口分署不同的逻辑网络。因此所有返回的流量也都经过负载均衡。这种方式对网络的改动小,能均衡任何下行流量。

2、桥接模式

桥接模式配置简单,不改变现有网络。负载均衡的WAN口和LAN口分别连接上行设备和下行服务器。LAN口不需要配置IP(WAN口与LAN口是桥连接),所有的服务器与负载均衡均在同一逻辑网络中。

由于这种安装方式容错性差,网络架构缺乏弹性,对广播风暴及其他生成树协议循环相关联的错误敏感,因此一般不推荐这种安装架构。

3、服务直接返回模式

这种安装方式负载均衡的LAN口不使用,WAN口与服务器在同一个网络中,互联网的客户端访问负载均衡的虚IP(VIP),虚IP对应负载均衡机的WAN口,负载均衡根据策略将流量分发到服务器上,服务器直接响应客户端的请求。因此对于客户端而言,响应他的IP不是负载均衡机的虚IP(VIP),而是服务器自身的IP地址。也就是说返回的流量是不经过负载均衡的。因此这种方式适用大流量高带宽要求的服务。

##Nginx反向代理负载均衡_负载均衡

接下来就用nginx来实操一下反向代理!!!!

1、WEB服务器环境准备

准备5台服务器

2、环境配置

1.克隆HA1/HA2/WEB1/WEB2/WEB3

2.开机启动 - 修改主机名字 - IP地址 - 修改软件源 - yum cache

3.WEB构建完毕-apache

HA1主机配置

[root@localhost ~]# hostnamectl set-hostname HA1 && bash
[root@ha1 ~]# mkdir /etc/yum.repos.d/bak
[root@ha1 ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
[root@ha1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && yum clean all && yum makecache
[root@ha1 html]# systemctl stop firewalld.service && systemctl disable firewalld.service

HA2主机配置

[root@localhost ~]# hostnamectl set-hostname HA2 && bash
[root@ha2 ~]# mkdir /etc/yum.repos.d/bak
[root@ha2 ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
[root@ha2 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && yum clean all && yum makecache
[root@ha2 html]# systemctl stop firewalld.service && systemctl disable firewalld.service

web1

[root@localhost ~]# hostnamectl set-hostname web1 && bash
[root@web1 ~]# mkdir /etc/yum.repos.d/bak
[root@web1 ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
[root@web1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && yum clean all && yum makecache
[root@web1 ~]# systemctl stop firewalld.service && systemctl disable firewalld.service
[root@web1 ~]# yum install -y httpd
[root@web1 ~]# echo "web-server1" >> /var/www/html/index.html
[root@web1 ~]# systemctl start httpd.service && systemctl enable httpd.service && systemctl status httpd.service

web2

[root@localhost ~]# hostnamectl set-hostname web2 && bash
[root@web2 ~]# mkdir /etc/yum.repos.d/bak
[root@web2 ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
[root@web2 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && yum clean all && yum makecache
[root@web2 ~]# systemctl stop firewalld.service && systemctl disable firewalld.service
[root@web2 ~]# yum install -y httpd
[root@web2 ~]# echo "web-server1" >> /var/www/html/index.html
[root@web2 ~]# systemctl start httpd.service && systemctl enable httpd.service && systemctl status httpd.service

web3

[root@localhost ~]# hostnamectl set-hostname web3 && bash
[root@web3 ~]# mkdir /etc/yum.repos.d/bak
[root@web3 ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
[root@web3 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && yum clean all && yum makecache
[root@web3 ~]# systemctl stop firewalld.service && systemctl disable firewalld.service
[root@web3 ~]# yum install -y httpd
[root@web3 ~]# echo "web-server1" >> /var/www/html/index.html
[root@web3 ~]# systemctl start httpd.service && systemctl enable httpd.service && systemctl status httpd.service

3、Nginx软件部署(只在两个HA节点上部署)

HA1

安装nginx软件

[root@ha1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ha1 ~]# yum install -y nginx nginx-mod-stream
[root@ha1 ~]# systemctl daemon-reload && systemctl start nginx && systemctl enable nginx && systemctl status nginx
[root@ha1 ~]# pstree -ap | grep -v grep | grep -i nginx
|-nginx,10687
| |-nginx,10688
| `-nginx,10689
[root@ha1 ~]# netstat -nltp | grep -i nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10687/nginx: master
tcp6 0 0 :::80 :::* LISTEN 10687/nginx: master

Nginx反向代理-负载均衡部署

[root@ha1 ~]# cd /etc/nginx/
[root@ha1 nginx]# mv nginx.conf nginx.conf.bak

修改配置文件

[root@ha1 nginx]# vim nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
# [alert] 18037#0: 1024 worker_connections are not enough
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024; # 最多可以建这么多工作节点
}

##### add to config start #####
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status
$upstream_bytes_sent';
access_log /var/log/nginx/web_cluster.log main;

# upstream-load_balance-Cluster 这个是负载均衡,下面就是负载均衡的配置
upstream web_cluster {
server 192.168.40.101:80; # server1 IP:port
server 192.168.40.102:80; # server2 IP:port
server 192.168.40.103:80; # server3 IP:port
}

server {
listen 80; # nginx proxy port -
proxy_pass web_cluster; # 添加这个就是反向代理
}

}
##### add to config end #####

注意:listen port 端⼝号码 - ⽤户访问访问端⼝

server1 IP:port 端⼝号码 - 后端服务的实际端⼝


检查Nginx配置⽂件格式

[root@ha1 nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful


重新加载配置⽂件

[root@ha1 nginx]# nginx -s reload

HA2

安装nginx软件

[root@ha2 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ha2 ~]# yum install -y nginx nginx-mod-stream
[root@ha2 ~]# systemctl daemon-reload && systemctl start nginx && systemctl enable nginx && systemctl status nginx
[root@ha2 ~]# pstree -ap | grep -v grep | grep -i nginx
[root@ha2 ~]# pstree -ap | grep -v grep | grep -i nginx
|-nginx,10361
| |-nginx,10362
| `-nginx,10363
[root@ha2 ~]# netstat -nltp | grep -i nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10361/nginx: master
tcp6 0 0 :::80 :::* LISTEN 10361/nginx: master


Nginx反向代理-负载均衡部署

[root@ha1 ~]# cd /etc/nginx/ [root@ha1 nginx]# mv nginx.conf nginx.conf.bak

修改配置文件

[root@ha1 nginx]# vim nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
# [alert] 18037#0: 1024 worker_connections are not enough
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024; # 最多可以建这么多工作节点
}

##### add to config start #####
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status
$upstream_bytes_sent';
access_log /var/log/nginx/web_cluster.log main;

# upstream-load_balance-Cluster 这个是负载均衡,下面就是负载均衡的配置
upstream web_cluster {
server 192.168.40.101:80; # server1 IP:port
server 192.168.40.102:80; # server2 IP:port
server 192.168.40.103:80; # server3 IP:port
}

server {
listen 80; # nginx proxy port -
proxy_pass web_cluster; # 添加这个就是反向代理
}

}


检查Nginx配置⽂件格式

[root@ha2 nginx]# nginx -t                                                                                                                                                                                                         
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载配置⽂件

[root@ha2 nginx]# nginx -s reload

标签:负载,nginx,##,etc,Nginx,systemctl,yum,&&,root
From: https://blog.51cto.com/u_15397018/6104876

相关文章

  • JSP内置对象
    简介JSP的内置对象是指在JSP页面系统中已经默认内置的Java对象,这些对象不需要开发人员显式声明即可使用。在JSP页面中,可以通过存取JSP内置对象实现与JSP页面和Servlet环境......
  • Matlab实现图像压缩
    文章和代码以及样例图片等相关资源,已经归档至【Github仓库:​​digital-image-processing-matlab​​】或者公众号【AIShareLab】回复数字图像处理也可获取。目的1.理解图......
  • linux基本功系列之mv命令实战
    前言linux常用命令之文件管理命令,目前已经进行到第10个,今天我们一起来看看mv命令。日常工作中,我们把文件移走或者改名都是用的mv命令1、MV命令的介绍mv命令来自于英文单词mo......
  • HTMLReport使用2 | HTMLReport使用方法详解
    (2|HTMLReport的使用)注:以下实例来源于官网。1日志为测试报告中添加过程日志;多线程下,在报告中会分别记录每个线程的日志,同时会产生与测试报告同名的测试log文件。......
  • 测试开发-怎么能忘记打卡呢-神器AppleScript
    写在前面昨天的主题反响不错,今天继续给大家介绍一款我自认为比较好用的东西,MacOS下的AppleScript,AppleScript像是mac电脑给开发者留下的一个入口一样,他不同于其他语言......
  • Linux基础命令使用
    1.CentOS安装和卸载Python3卸载python3rpm-qa|greppython3|xargsrpm-ev--allmatches--nodeps卸载pyhton3whereispython3|xargsrm-frv删除所有残余文件......
  • Learn Git Branching——学习Git图形演示
    地址https://oschina.gitee.io/learn-git-branching 操作指令1、reset:重新开始2、hint:显示指令提示3、objective:显示要求4、showsolution:显示答案5、levels:选择......
  • python操作pandas的笔记
    importpandasaspddata={'name':['Alice','Bob','Charlie','David'],'age':[25,30,35,40],'gender':['F','M','M','M'......
  • 帝国CMS:数据字段存为了文本格式,如何进行批量替换?
    帝国CMS数据库中,形如以下内容,表示newstext内容保存到了txt文本,这样做的好处是相对于大量文字内容的,可以快速的读取渲染。但是弊端是不方便批量进行数据修改。  对于这......
  • 服务不停宕机或重启后的测试要点
    一、机器环境情况机器A和B上分别起着服务A和B,服务A和B之间通过tcp协议通信。二、测试情况分析情况一:B机器不停服务直接关机情况下(shutdown机器),服务A处理是否正常......