您好,登录后才能下订单哦!
下文给大家带来,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用亿速云在行业内累计的经验来做一个解答。
配置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版本:
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/Linuxnginx版本:
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安装部署
更新软件包:
root@ubuntu:~# apt-get update
安装依赖软件:
root@ubuntu:~# apt -y install openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev make gcc下载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端口是否开启下载镜像:
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更新软件包:
[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
nginx2nginx1修改内容如下:
 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.nginx2.com;            //设置域名
#charset koi8-r;
#access_log  logs/host.access.log  main;
location / {
    root   html;
    index  index.html index.htm;
    }
}要开始使用NGINX Plus或NGINX开源对一组服务器的HTTP流量进行负载均衡,使用upstream指令定义该组,该指令放置在http上下文中,proxy_pass指令用来转发请求到后端一般指定在loction上下文中
基本配置方法:
http {
    upstream nginx {            #test为组名
        server www.nginx1.com ;     #server用来指定后端服务器的访问地址,一般指定域名、ip、端口,域名和ip二选一,端口可忽略
        server www.nginx2.com weight=5;     #weight指定权重值
        server www.nginx3.com down;         #down用来停止对此服务器的转发
        ......省略其他配置
    }
    server{
        location / {
            proxy_pass http://nginx;        #http://nginx为转发那个组的后端服务器
            ......省略其他配置
        }
        ......省略其他配置
    }
}nginx做负载均衡官方提供了4种方法,下面逐一介绍这四种方法:
请求在服务器之间平均分配,同时考虑了服务器权重。默认情况下使用此方法(没有启用它的指令)
默认配置就是Round Robin,配置方法:
http {
upstream nginx {            #test为组名
    server www.nginx1.com ;     #server用来指定后端服务器的访问地址
    server www.nginx2.com ;     #weight指定权重值
    ......省略其他配置
}
server{
    location / {
        proxy_pass http://nginx;        #http://nginx为转发那个组的后端服务器
        ......省略其他配置
    }
    ......省略其他配置
}
}将活动连接最少的请求发送到服务器,也是在考虑到服务器的权重问题才设置的这种方法
配置方法:
http {
upstream nginx {            #test为组名
       less_conn ; 
    server www.nginx1.com ;     #server用来指定后端服务器的访问地址
    server www.nginx2.com ;     #weight指定权重值
    ......省略其他配置
}
server{
    location / {
        proxy_pass http://nginx;        #http:#nginx为转发那个组的后端服务器
        ......省略其他配置
    }
    ......省略其他配置
}
}从客户端IP地址确定向其发送请求的服务器。在这种情况下,可以使用IPv4地址的前三个八位位组或整个IPv6地址来计算哈希值。该方法保证了来自同一地址的请求将到达同一服务器,除非它不可用。
配置方法:
http {
upstream nginx {            #test为组名
       ip_hash ; 
    server www.nginx1.com ;     #server用来指定后端服务器的访问地址
    server www.nginx2.com ;     #weight指定权重值
    ......省略其他配置
}
server{
    location / {
        proxy_pass http://nginx;        #http:#nginx为转发那个组的后端服务器
        ......省略其他配置
    }
    ......省略其他配置
}
}向其发送请求的服务器是根据用户定义的键确定的,该键可以是文本字符串,变量或组合。
配置方法:
http {
upstream nginx {            #test为组名
      hash $request_uri consistent;
    server www.nginx1.com ;     #server用来指定后端服务器的访问地址
    server www.nginx2.com ;     #weight指定权重值
    ......省略其他配置
}
server{
    location / {
        proxy_pass http://nginx;        #http://nginx为转发那个组的后端服务器
        ......省略其他配置
    }
    ......省略其他配置
}
}看了以上关于nginx跨多个应用程序实例的负载均衡介绍,如果大家还有什么地方需要了解的可以在亿速云行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,亿速云技术工程师在行业内拥有十几年的经验了。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。