nginx跨多个应用程序实例的负载平衡是一种用于优化资源利用率,最大化吞吐量,减少延迟和确保容错配置的常用技术。

环境介绍

配置nginx负载均衡器因会用到多台服务器来进行,所以下面我会用到docker,具体docker的使用请移步docker实战

  • 系统环境:

     root@ubuntu:~# lsb_release  -a			#查看系统版本
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description: Ubuntu 19.10
    Release: 19.10
    Codename: eoan
    root@ubuntu:~# uname -a #查看系统是多少位
    Linux ubuntu 5.3.0-18-generic #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  • docker版本和相关操作系统版本

     docker版本:
    root@ubuntu:~# docker --version #查看docker版本
    Docker version 19.03.3, build a872fc2f86 操作系统版本:
    [root@57b669db1de1 /]# cat /etc/redhat-release #查看系统版本
    CentOS Linux release 8.0.1905 (Core)
    [root@57b669db1de1 /]# uname -a
    Linux 57b669db1de1 5.3.0-18-generic #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  • 软件版本:

     nginx版本:
    root@ubuntu:~# nginx -v 查看nginx版本
    nginx version: nginx/1.16.1 (Ubuntu) docker中nginx版本:
    [root@57b669db1de1 /]# nginx -v #查看nginx版本
    nginx version: nginx/1.14.1

安装nginx

具体nginx的安装请参考nginx安装部署

  • 安装依赖软件

     更新软件包:
    root@ubuntu:~# apt-get update 安装依赖软件:
    root@ubuntu:~# apt -y install openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev make gcc
  • 编译安装nginx

     下载nginx:
    root@ubuntu:~# wget http:#nginx.org/download/nginx-1.17.6.tar.gz 解压:
    root@ubuntu:/opt# tar zxf nginx-1.17.6.tar.gz
    root@ubuntu:/opt# cd nginx-1.17.6/
    root@ubuntu:/opt/nginx-1.17.6# ls #列出目录
    auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
    root@ubuntu:/opt/nginx-1.17.6# 配置:
    root@ubuntu:/opt/nginx-1.17.6# ./configure --prefix=/usr/local/nginx
    编译以及部署:
    root@ubuntu:/opt/nginx-1.17.6# make && make install
    root@ubuntu:/opt/nginx-1.17.6# cd /usr/local/nginx/
    root@ubuntu:/usr/local/nginx# ls
    conf html logs sbin 启动:
    root@ubuntu:/usr/local/nginx# ln sbin/nginx /usr/local/sbin/ #创建nginx软连接,设置为命令
    root@ubuntu:/usr/local/nginx# nginx #启动nginx
    root@ubuntu:/usr/local/nginx# netstat -anpl | grep nginx #查看nginx端口是否开启
    root@ubuntu:/usr/local/nginx# lsof -i:80 #查看80端口是否开启

docker安装nginx

  • 创建容器

     下载镜像:
    root@ubuntu:~# docker pull registry.cn-beijing.aliyuncs.com/blxt/centos 创建容器并启动:
    root@ubuntu:~#
    root@ubuntu:~# docker run -itd --name nginx1 --privileged registry.cn-beijing.aliyuncs.com/blxt/centos /sbin/init
    root@ubuntu:~# docker ps -a #查看容器是否创建并启动
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAME
    6801d55682a3 registry.cn-beijing.aliyuncs.com/blxt/centos "/sbin/init" 5 minutes ago Up 5 minutes nginx1 相同方法创建第二个容器:
    root@ubuntu:~# docker run -itd --name nginx2 --privileged registry.cn-beijing.aliyuncs.com/blxt/centos /sbin/init
    root@ubuntu:~# docker ps -a
    root@ubuntu:~#
    root@ubuntu:~# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    1d31d3fc0ec1 registry.cn-beijing.aliyuncs.com/blxt/centos "/sbin/init" 4 minutes ago Up 4 minutes nginx2
    6801d55682a3 registry.cn-beijing.aliyuncs.com/blxt/centos "/sbin/init" 5 minutes ago Up 5 minutes nginx1 连接到容器:
    root@ubuntu:~# docker exec -it nginx1 /bin/bash
    root@ubuntu:~# docker exec -it nginx2 /bin/bash
    [root@6801d55682a3 /]# hostname nginx1 #更改主机名称
    [root@6801d55682a3 /]# bash #使更改的名称生效
    [root@1d31d3fc0ec1 /]# hostname nginx2
    [root@1d31d3fc0ec1 /]# bash
  • 安装nginx

     更新软件包:
    [root@nginx1 /]# yum clean all #清空缓存
    [root@nginx1 /]# yum makecache #更新软件包 安装nginx:
    [root@nginx1 /]# yum -y install nginx
    [root@nginx1 /]# rpm -qa | grep nginx #查看是否已经安装 启动nginx:
    [root@nginx1 /]# systemctl start nginx
    [root@nginx1 /]# netstat -anpl | grep nginx #查看nginxnn端口是否开启
    [root@nginx1 /]# lsof -i:80 #查看80端口是否开启
    安装第二个nginx使用相同方法即可!此处省略!!! 访问nginx:
    [root@nginx1 /]# curl 127.0.0.1
  • 修改网页内容

     查看网页存在目录:
    [root@nginx1 nginx]# more nginx.conf #查看配置文件
    在配置文件中找到下面root行,后面目录即网页文件存放目录
    root /usr/share/nginx/html;
    [root@nginx1 html]# cd /usr/share/nginx/html/
    [root@nginx1 html]# ls #列出目录内容
    404.html 50x.html index.html nginx-logo.png poweredby.png
    [root@nginx1 html]# echo nginx1 > index.html #清空nginx主页文件内容,并重新写入内容为nginx1
    nginx2如同,注意不要设置一样的主页内容 访问nginx:
    [root@nginx1 html]# curl 127.0.0.1
    nginx1
    [root@nginx2 ~]# curl 127.0.0.1
    nginx2
  • 修改配置文件

     nginx1修改内容如下:
    server {
    listen 80;
    server_name www.nginx1.com; //设置域名 #charset koi8-r; #access_log logs/host.access.log main; location / {
    root html;
    index index.html index.htm;
    }
    } nginx2修改内容如下:
    server {
    listen 80;
    server_name www.nginx.com; //设置域名 #charset koi8-r; #access_log logs/host.access.log main; location / {
    root html;
    index index.html index.htm;
    }
    }

配置http负载均衡器

要开始使用NGINX Plus或NGINX开源对一组服务器的HTTP流量进行负载均衡,使用upstream指令定义该组,该指令放置在http上下文中,proxy_pass指令用来转发请求到后端一般指定在loction上下文中
基本配置方法:

http {
upstream nginx { #test为组名
server www.nginx1.com ; #server用来指定后端服务器的访问地址,一般指定域名、ip、端口,域名和ip二选一,端口可忽略
server www.nginx.com weight=5; #weight指定权重值
server www.nginx4.com down; #down用来停止对此服务器的转发
......省略其他配置
}
server{
location / {
proxy_pass http://nginx; #http://nginx为转发那个组的后端服务器
......省略其他配置
}
......省略其他配置
}
}

nginx做负载均衡官方提供了4种方法,下面逐一介绍这四种方法:

  • Round Robin

请求在服务器之间平均分配,同时考虑了服务器权重。默认情况下使用此方法(没有启用它的指令)
默认配置就是Round Robin,配置方法:

http {
upstream nginx { #test为组名
server www.nginx1.com ; #server用来指定后端服务器的访问地址
server www.nginx.com ; #weight指定权重值
......省略其他配置
}
server{
location / {
proxy_pass http://nginx; #http://nginx为转发那个组的后端服务器
......省略其他配置
}
......省略其他配置
}
}
  • 最少连接

将活动连接最少的请求发送到服务器,也是在考虑到服务器的权重问题才设置的这种方法
配置方法:

http {
upstream nginx { #test为组名
less_conn ;
server www.nginx1.com ; #server用来指定后端服务器的访问地址
server www.nginx.com ; #weight指定权重值
......省略其他配置
}
server{
location / {
proxy_pass http://nginx; #http:#nginx为转发那个组的后端服务器
......省略其他配置
}
......省略其他配置
}
}
  • ip_hash

从客户端IP地址确定向其发送请求的服务器。在这种情况下,可以使用IPv4地址的前三个八位位组或整个IPv6地址来计算哈希值。该方法保证了来自同一地址的请求将到达同一服务器,除非它不可用。
配置方法:

http {
upstream nginx { #test为组名
ip_hash ;
server www.nginx1.com ; #server用来指定后端服务器的访问地址
server www.nginx.com ; #weight指定权重值
......省略其他配置
}
server{
location / {
proxy_pass http://nginx; #http:#nginx为转发那个组的后端服务器
......省略其他配置
}
......省略其他配置
}
}
  • hash

向其发送请求的服务器是根据用户定义的键确定的,该键可以是文本字符串,变量或组合。
配置方法:

http {
upstream nginx { #test为组名
hash $request_uri consistent;
server www.nginx1.com ; #server用来指定后端服务器的访问地址
server www.nginx.com ; #weight指定权重值
......省略其他配置
}
server{
location / {
proxy_pass http://nginx; #http://nginx为转发那个组的后端服务器
......省略其他配置
}
......省略其他配置
}
}

带你玩转nginx负载均衡的更多相关文章

  1. 手把手教你玩转nginx负载均衡(二)----安装虚拟机操作系统

    引言 在上一篇,我们组装好了虚拟机的硬件部分,那么现在我们就要把操作系统装上了,既然是服务器,那么安装linux操作系统是个比较好的选择,如果你喜欢的话,安装windows也是没有任何问题的 我这里选 ...

  2. 手把手教你玩转nginx负载均衡(五)----配置后端服务器组

    引言 在前面几篇中,我们成功的搭建起了一台nginx服务器,所以我们要重复前面的步骤,把服务器的数量增加到3台以上,我这里已经建好了另外两台,分别是centos7-22,centos7-23,对应的i ...

  3. 手把手教你玩转nginx负载均衡(三)----配置虚拟服务器网络

    引言 虽然上一篇我们成功的启动了虚拟机,也安装好了操作系统,但是这台虚拟机和主机以及其他虚拟机是没有办法连通的,我们的目标是配置多台服务器并且配置nginx反向代理,来实现负载均衡,所以不能访问内网是 ...

  4. 手把手教你玩转nginx负载均衡(四)--源码安装nginx

    引言: 在上一篇,我们已经装好了虚拟机,并且已经配置好了网络,那么今天我们就要开始安装nginx服务器了. 安装工具以及过程 安装gcc编译套件以及nginx依赖模块 yum -y install g ...

  5. 手把手教你玩转nginx负载均衡(一)----使用vitualBox创建虚拟机

    引言 作为一个web程序员,有时候需要想尽办法来利用有限的资源来产生最大程度的负载,除了提高硬件配置,增加带宽之外,CDN加速,DNS加速,缓存,还可以利用反向代理.但是要说反向代理,就不的不说ngi ...

  6. Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用

    上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx ...

  7. 161028、Nginx负载均衡实现tomcat集群方案简要小结

    重点两部分:一.负载均衡二.tomcat集群 所谓tomcat集群,就是可以向外提供并行服务的多台机器,任何一台服务器宕机,其它服务器可以替代它向外提供服务,而不影响用户访问. Nginx是一个常用的 ...

  8. linux下nginx负载均衡部署

    nginx负载均衡部署 Nginx("engine x") 是一个高性能的 HTTP 和 反向代理 server,也是一个 IMAP/POP3/SMTP 代理server. Ngi ...

  9. Keepalived+LVS+Nginx负载均衡之高可用

    Keepalived+LVS+Nginx负载均衡之高可用 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常 ...

  10. LVS+nginx负载均衡知识点1

    lvs+nginx负载均衡 1       学习目标 掌握什么是负载均衡及负载均衡的作用和意义. 了解lvs负载均衡的三种模式. 了解lvs-DR负载均衡部署方法. 掌握nginx实现负载均衡的方法. ...

随机推荐

  1. 【转载】【转载视频】 如何成为好的IT技术面试官

    在外网找到了一个IT技术面试官的面试心得,感觉还不错,挺有借鉴的,这里mark一下. 地址: https://www.youtube.com/watch?v=yFMmkoqDPlM ========= ...

  2. baselines算法库baselines/common/input.py模块分析

    baselines算法库baselines/common/input.py模块代码: import numpy as np import tensorflow as tf from gym.space ...

  3. 安装华为软件昇腾mindspore-gpu-1.9.0失败记录

    官网安装地址: https://www.mindspore.cn/install PS: 不得不说华为的软件是愈发的不好用了,这个mindspore老版本去年我是使用过的,安装也是比较方便的,搞不清这 ...

  4. 读论文《Reinforced Attention for Few-Shot Learning and Beyond》

    2022年4月22日,实验室开组会,我讲了论文<Reinforced Attention for Few-Shot Learning and Beyond>,最近整理资料又再读了一遍,这里 ...

  5. vue之父组件与子组件的通信

    1.背景 参数传递(多理解) 1.父传子<c-parent :messagevue="message"></c-parent>,请看下面具体的截图描述 2. ...

  6. 旧物利用 - 将机顶盒改造为一台Linux开发机!

    前言 机顶盒型号:移动魔百盒CM201-2(CH),芯片组: hi3798mv300(hi3798mv3dmm),其他型号类似 理论上适用于以下SOC:Hi3798Mv100 / Hi3798Cv20 ...

  7. Go 互斥锁 Mutex 源码分析 (一)

    0. 前言 锁作为并发编程中的关键一环,是应该要深入掌握的. 1. 锁 1.1 示例 实现锁很简单,示例如下: var global int func main() { var mu sync.Mut ...

  8. LaTeX 书写函数

    \[\text{text 模式} \] \[\mathrm{mathrm 模式} \] \[\textit{textit 模式} \] \[\operatorname{operatorname 模式} ...

  9. 一文了解JSON

    目录 JSON 在JavaScript 中的使用. json 的定义 json 的访问 json 的两个常用方法 JSON 在 在 java 中的使用 javaBean 和 和 json 的互转 Li ...

  10. Linux firewalld 的用法

    大乌龙事件之防火墙作祟: 在我的初始化环境中 selinux.iptables.firewalld 是关闭的, 莫名的firewalld 是没有关闭的这是我在安装完docker后一段时间后部署jenk ...