怎么样实现nginx在http的负载均衡

发布时间:2020-04-16 14:04:11 作者:三月
来源:亿速云 阅读:222

下文给大家带来怎么样实现nginx在http的负载均衡,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用亿速云在行业内累计的经验来做一个解答。

首先复习一下LB Cluster负载均衡集群

四层:

LVS

Nginx(stream)

Haproxy(mode_tcp)

七层

Http protocol

Nginx(http,upstream)

Haproxy(mode http)

Httpd/ats/perlbal/pound/…

怎么样实现nginx在http的负载均衡

接下来如何实现nginx在http的负载均衡


ngx_stream_proxy_module模块能为http服务做调度,其中stream模块中有

专门的server子命令,不同于其他server,其他server是用于定义虚拟主机的

而stream模块中的server是用来定义组中的一台云服务器的,server可以重复使用多次,

定义多台服务器,因此可实现服务器的负载均衡。

#################################################################

1、实验环境准备,至少准备三台主机,其中一台做为nginx调度服务器,装备两块网卡

分别在三台主机上配置nginx,httpd和httpd,并测试可以成功访问页面

[root@localhost nginx]# curl http://172.18.10.10:80/test1.html

Test Page 1 on UpStream Server 1 (172.18.10.10)

[root@localhost nginx]# curl http://172.18.10.11:80/test1.html

Test Page 1 on UpStream Server 2 (172.18.10.11)


将172.18.10.10和172.18.10.11做为动态站点(安装httpd+php,即ap,listen 80)

将172.18.10.10和172.18.10.11做为静态站点(其中10.11安装nginx,监听8080,10.10配置虚拟主机,监听80和8080)


2、实验目的。实现nginx对静态内容和动态内容的负载均衡

3、开始配置操作

在172.18.10.10下编辑php页面

[root@localhost ~]# vim /var/www/html/index.php

<h3>HTTPD listend on 80 Server1</h3>

<?php

        phpinfo();

?>

将实验页面发至172.18.10.11的页面文件存放路径下

[root@localhost ~]# scp /var/www/html/index.php 172.18.10.11:/var/www/html/

修改Server1为Server2

<h3>HTTPD listend on 80 Server2</h3>

<?php

        phpinfo();

?>


4、常识使用谷歌浏览器请求两个地址,看看是否测试页面能够正常显示--------经测试发现能够正常显示

5、配置静态站点的nginx

将准备好的nginx安装包分别scp到两台主机上

[root@localhost ~]# scp nginx-1.6.2-1.el6.ngx.x86_64.rpm 172.18.10.10:/root/

6、安装nginx

[root@localhost ~]# yum install nginx-1.6.2-1.el6.ngx.x86_64.rpm

7、配置静态站点的虚拟服务

172.18.10.10上:

注释DocumentRoot路径

#DocumentRoot "/var/www/html"

添加新的监听端口

#Listen 12.34.56.78:80

Listen 80

Listen 8080

添加虚拟主机,分别监听在80和8080端口上

<VirtualHost *:80>

    DocumentRoot /var/www/shope

    ServerName www.magedu.com

</VirtualHost>

<VirtualHost *:8080>

    DocumentRoot /var/www/html

    ServerName imgs.magedu.com

</VirtualHost>

保存退出

创建目录

[root@localhost ~]# mkdir /var/www/shope

将index.php移至该目录下

[root@localhost ~]# mv /var/www/html/index.php /var/www/shope/

检查语法                  

[root@localhost ~]# httpd -t

重启httpd服务

[root@localhost ~]# service httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName

                                                           [  OK  ]

在浏览器端分别访问测试80和8080端口

结果正常

怎么样实现nginx在http的负载均衡

172.18.10.11上:

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf

将虚拟主机监听端口改为8080

    listen       80———————------》 listen       8080;


更改root路径

root   /usr/share/nginx/html;—————》root   /data/html;

创建虚拟主机目录路径

[root@localhost ~]# mkdir /data/html -pv

mkdir: created directory `/data'

mkdir: created directory `/data/html'

将所有test文件移至/data/html目录下

[root@localhost ~]# mv /var/www/html/test* /data/html/

启动nginx服务,并且查看是否端口监听

[root@localhost ~]# nginx

[root@localhost ~]# ss -tnl

State      Recv-Q Send-Q                                        Local Address:Port                                          Peer Address:Port 

LISTEN     0      128                                                       *:8080                                                     *:*     

LISTEN     0      128                                                      :::80                                                      :::*     

LISTEN     0      128                                                      :::22                                                      :::*     

LISTEN     0      128                                                       *:22                                                       *:*     

LISTEN     0      100                                                     ::1:25                                                      :::*     

LISTEN     0      100                                               127.0.0.1:25  

访问页面。看看是否能够正常访问

8、配置nginx调度端的nginx服务在172.18.200.100上

[root@localhost ~]# vim /etc/nginx/nginx.conf 

#使用默认的加权轮询算法,进行会发绑定

upstream websrvs {

                

                server 172.18.10.10:80 weight=2 max_fails=2 fail_timeout=2;

                server 172.18.10.11:80 weight=3;

        }

        upstream staticsrvs {

                

                server 172.18.10.10:8080 weight=1;

                server 172.18.10.11:8080 weight=1;

        }

9、编辑调度的方法

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf

       index index.php index.html; ####全局定义,先后顺序

    location / {

        proxy_pass http://websrvs;   ####动态资源加载路径定义

        root   /usr/share/nginx/html;  

        index index.php index.html index.htm;

    }

        location ~* \.(jpg|jpeg|png|gif|html)$ {

                proxy_pass http://staticsrvs;   #####静态资源加载路径定义

                index index.php;

        }

10、从新加载测试

[root@localhost ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@localhost ~]# nginx -s reload

打开谷歌浏览器,输入http://172.18.200.100/刷新页面发现会有如下的页面内容来回切换

HTTPD listend on 80 Server2


HTTPD listend on 80 Server1

请求http://172.18.200.100/index.php,发现也是如下页面内容来回切换

HTTPD listend on 80 Server2


HTTPD listend on 80 Server1

从其他客户端使用curl来测试

[root@localhost ~]# for ((i=1;i<=10;i++));do curl http://172.18.200.100/index.php; done

测试结果:实现动态内容的负载均衡


接下来请求静态文件:http://172.18.200.100/test1.html,不断刷新,发现得到如下内容来回切换

Test Page 1 on UpStream Server 1 (172.18.10.10)

Test Page 1 on UpStream Server 2 (172.18.10.11)


从其他客户端使用curl来测试

[root@localhost ~]# for ((i=1;i<=10;i++));do curl http://172.18.200.100/test1.html; done

Test Page 1 on UpStream Server 1 (172.18.10.10)

Test Page 1 on UpStream Server 2 (172.18.10.11)

Test Page 1 on UpStream Server 1 (172.18.10.10)

Test Page 1 on UpStream Server 2 (172.18.10.11)

Test Page 1 on UpStream Server 1 (172.18.10.10)

Test Page 1 on UpStream Server 2 (172.18.10.11)

Test Page 1 on UpStream Server 1 (172.18.10.10)

Test Page 1 on UpStream Server 2 (172.18.10.11)

Test Page 1 on UpStream Server 1 (172.18.10.10)

Test Page 1 on UpStream Server 2 (172.18.10.11)

测试结果:实现动态内容的负责均衡

最终实现动静分离,而在静态内容上面我们还可以定义缓存,提升效率。

看了以上关于怎么样实现nginx在http的负载均衡,如果大家还有什么地方需要了解的可以在亿速云行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,亿速云技术工程师在行业内拥有十几年的经验了。亿速云官网链接www.yisu.com

 



推荐阅读:
  1. nginx配置http负载均衡是怎么样的
  2. Nginx是怎么实现负载均衡的?Nginx负载均衡实现案例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx 负载均衡 ginx

上一篇:云服务器大数据中心是什么?有什么特点

下一篇:云服务器不稳定的原因及解决办法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》