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. OpenAI内讧更多细节曝光:奥特曼离间董事会失败

    参考: https://www.thepaper.cn/newsDetail_forward_25512687 ============================== 根据 https://ww ...

  2. 2023年3月份至2024年3月份CCF会议情况——人工智能领域

    April 2, 2023:          https://2023.ecmlpkdd.org/submissions/key-dates-deadlines/ ECML-PKDD (Europe ...

  3. baselines算法库common/vec_env/util.py模块分析

    util.py模块代码: """ Helpers for dealing with vectorized environments. """ ...

  4. 多线程之park()与interrupt()的理解

    1.背景 其他不多说,很多时候面试会问 2.代码 package com.ldp.demo01; import com.common.MyThreadUtil; import lombok.exter ...

  5. 用一杯星巴克的钱,训练自己私有化的ChatGPT

    文章摘要:用一杯星巴克的钱,自己动手2小时的时间,就可以拥有自己训练的开源大模型,并可以根据不同的训练数据方向加强各种不同的技能,医疗.编程.炒股.恋爱,让你的大模型更"懂"你-. ...

  6. 【LCA 树上两点的距离 判定点是否在某条边中】洛谷P3398 仓鼠找sugar

    题目链接:P3398 仓鼠找 sugar - 洛谷 | (luogu.com.cn) 题目大意:判定一棵树上的两条边是否相交 Tag: [LCA] [树上两点间距离的计算] [如何判断与点在某条路径上 ...

  7. 万字长文带你了解Java日志框架使用Java日志框架

    大家好,我是晓凡 一.日志概念 日志的重要性不用我多说了,日志,简单来说就是记录. 用来记录程序运行时发生的事情.比如,程序启动了.执行了某个操作.遇到了问题等等,这些都可以通过日志记录下来. 想象一 ...

  8. 热力学平衡、Liftshitz 理论和朗道理论

    科学家们经过广泛的实验发现:熔化往往始于固体表面.熔化时,体系由 "固体-气体接触" 变为 "固体-熔化层接触 + 熔化层-气体接触".如果后者的能量更稳定,则 ...

  9. Win32封装对话框类

    [主程序入口.cpp] #include <windows.h> #include <tchar.h> #include "resource.h" #inc ...

  10. Maven 换源

    ~/.m2/settings.xml <settings> ... <mirrors> ... <mirror> <id>alimaven</id ...