网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

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

成都创新互联2013年开创至今,是专业互联网技术服务公司,拥有项目网站设计、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元钦南做网站,已为上家服务,为钦南各地企业和个人服务,联系电话:18982081108

首先复习一下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

HTTPD listend on 80 Server1

        phpinfo();

?>

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

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

修改Server1为Server2

HTTPD listend on 80 Server2

        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端口上

    DocumentRoot /var/www/shope

    ServerName www.magedu.com

    DocumentRoot /var/www/html

    ServerName imgs.magedu.com

保存退出

创建目录

[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.cdcxhl.com

 


分享文章:怎么样实现nginx在http的负载均衡
网址分享:http://bjjierui.cn/article/gchced.html

其他资讯