千家信息网

怎么样使用nginx+Apache负载均衡及动静分离

发表于:2024-10-03 作者:千家信息网编辑
千家信息网最后更新 2024年10月03日,下文给大家带来怎么样使用nginx+Apache负载均衡及动静分离 ,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验来做
千家信息网最后更新 2024年10月03日怎么样使用nginx+Apache负载均衡及动静分离

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


使用nginx+Apache负载均衡及动静分离

介绍

LB负载均衡集群分两类: LVS (四层)和 nginx或haproxy (七层)

客户端都是通过访问分发器的VIP来访问网站 在七层中的网站页面有: .php .html .png .jpeg .jsp 等, 有态页面有静态页面。 需要在应用层基于同的应用行分发。

一:实验拓扑图:

二:实验目标

实战:使用Apache+nginx实现静分离的负载均衡集群

三:实验环境

主机作用分类

主机名

IP地址

安装软件

Nginx,代理云服务器

xuegod63.cn

192.168.1.63

nginx-1.8.0.tar.gz

Apache,静态页面处理

xuegod62.cn

192.168.1.62

http

Apache,静态页面处理

xuegod64.cn

192.168.1.64

http

Apache,动态页面处理

xuegod62.cn

192.168.1.62

http

Apache,动态页面处理

xuegod64.cn

192.168.1.64

http

Apache,图片处理

xuegod62.cn

192.168.1.62

http

Apache,图片处理

xuegod64.cn

192.168.1.64

http

四:实验代码

1、配置分发器xuegod63(代理服务器)

1安装nginx时必须先安装相应的编译工具

[root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake

[root@xuegod63 ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

zlib: nginx提供gzip模块,需要zlib库支持

openssl:nginx提供ssl功能

pcre:支持地址重写rewrite功能

2安装nginx

[root@xuegod63 ~]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/src/

[root@xuegod63 ~]# cd /usr/local/src/nginx-1.8.0/

[root@xuegod63 nginx-1.8.0]# ./configure --prefix=/server/nginx-1.8.0 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module

参数解释

--with-http_dav_module ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPYMOVE方法)默认情冴下为关闭,需编译开

--with-http_stub_status_module ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)

--with-http_addition_module ngx_http_addition_module支持(作为一个输出过滤器,支持完全缓冲,分部分响应请求)

--with-http_sub_module ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)

--with-http_flv_module ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)

--with-http_mp4_module 用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件)

3编译和安装:

[root@xuegod63 nginx-1.8.0]#make -j 4

[root@xuegod63 nginx-1.8.0]#make install

4生成运行nginx的用户:

[root@xuegod63 nginx-1.8.0]# useradd -u 8000 -s /sbin/nologin nginx

5)启动nginx

[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx

[root@xuegod63 nginx-1.8.0]# echo '/server/nginx-1.8.0/sbin/nginx & ' >> /etc/rc.local

6测试: http://192.168.1.63/

7nginx服务日常操作: :

1测试配置文件是否正确语法:

[root@xuegod63 nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -t

nginx: the configuration file /server/nginx-1.8.0/conf/nginx.conf syntax is ok

nginx: configuration file /server/nginx-1.8.0/conf/nginx.conf test is successful

2重新加载配置文件

[root@xuegod63 nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -s reload

3关闭与开启nginx

[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s stop

[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s start #没有start参数

nginx: invalid option: "-s start"

7配置nginx成为分发器,实现静分离

[root@xuegod63 conf]# cd /server/nginx-1.8.0/conf #配置文件目录

[root@xuegod63 conf]# cp nginx.conf nginx.conf.back #备份一下配置文件

[root@xuegod63 conf]# vim nginx.conf

[root@xuegod63 nginx-1.8.0]# vim /server/nginx-1.8.0/conf/nginx.conf #启动nginx用户

改:# user nobody;

为:user nginx nginx;

改:

43 location / {

44 root html;

45 index index.html index.htm; #location / { 。。。} 中添加以下内容 #定义分发策略

index index.html index.htm;

if ($request_uri ~* \.html$){

proxy_pass http://htmlservers; #匹配到htm静态类型访问的都会转到html服务池中

}

if ($request_uri ~* \.php$){

proxy_pass http://phpservers; #匹配到php动态类型文件直接在nginx服务器上解析了

}

}

如图:

一下内容注释掉,匹配到php动态类型文件直接在nginx服务器上解析了,再解析给后端服务器:

如图:

8在配置文件nginx.conf的最后一行}前,添加以下内容:

upstream htmlservers { #定义静态文件负载均衡服务器组名称

server 192.168.1.62:80;

server 192.168.1.64:80;

}

upstream phpservers{ #定义动态文件负载均衡服务器组名称

server 192.168.1.62:80;

server 192.168.1.64:80;

}

upstream picservers { #定义图片文件负载均衡服务器组名称

server 192.168.1.62:80;

server 192.168.1.64:80;

}

#后期工作中,根据工作中的需要,配置成具体业务的IP地址

如图:

9重新加载nginx服务器配置文件:

[root@xuegod63 conf]# /server/nginx-1.8.0/sbin/nginx -t

nginx: the configuration file /server/nginx-1.8.0/conf/nginx.conf syntax is ok

nginx: configuration file /server/nginx-1.8.0/conf/nginx.conf test is successful

[root@xuegod63 conf]# /server/nginx-1.8.0/sbin/nginx -s reload

2配置后端服务器: xuegod62

1配置web服务器:

[root@xuegod62 html]# yum install httpd php -y

2生成静态测试文件:

root@xuegod62 html]#echo 192.168.1.62 > /var/www/html/index.html

3生成态测试文件:

[root@xuegod62 html]#vim /var/www/html/test.php #写如以下内容:

192.168.1.62-php

phpinfo();

?>

4生成图片文件: 上传如下图片,到"xuegod62网站/var/www/html/目录下:

5)启动apache服务器:

[root@xuegod62 html]# service httpd restart

3配置后端服务器xuegod64

1配置web服务器:

[root@xuegod64 html]# yum install httpd php -y

2生成静态测试文件:

[root@xuegod64 html]#echo 192.168.1.64 > /var/www/html/index.html

3生成态测试文件:

[root@xuegod64 html]#vim /var/www/html/test.php #写如以下内容:

192.168.1.64-php

phpinfo();

?>

4生成图片文件: --上传如下图片,到"xuegod64网站/var/www/html/目录下:

5)重启apache服务器

[root@xuegod64 html]# service httpd restart

4、测试

1测试负载均衡及动静分离---静态页面:

=

2测试动静分离及负载均衡---态页面:


3测试图片负载均衡


4测试自剔除坏的节点:

[root@xuegod64 html]# service httpd stop

http://192.168.1.63/pic.jpg

5测试性能:

扩展: 文件打开数过多

[root@xuegod63 html]# ab -n 1000 -c 1000 http://192.168.1.62/index.html #运行正常

[root@xuegod63 html]# ab -n 2000 -c 2000 http://192.168.1.62/index.html #报错

This is ApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.62 (be patient)

socket: Too many open files (24) # 测试时,一次打开的socket文件太多。

[root@xuegod63 ~]# ulimit -n

1024#系统默认一个程最多同时允许打开1024的文件

[root@xuegod63 ~]# ulimit -n 10240 #修改默认允许同事打开10240个文件

[root@xuegod63 ~]# ab -n 2000 -c 2000 http://192.168.1.62/index.html

This is ApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.62 (be patient)

Completed 200 requests

Completed 400 requests

.......

Completed 1800 requests

Completed 2000 requests

Finished 2000 requests

Server Software: Apache/2.2.15

Server Hostname: 192.168.1.62

Server Port: 80

Document Path: /index.html

Document Length: 13 bytes

Concurrency Level: 2000

Time taken for tests: 1.119 seconds

Complete requests: 2000

Failed requests: 0

Write errors: 0

Total transferred: 560000 bytes

HTML transferred: 26000 bytes

Requests per second: 1787.69 [#/sec] (mean)

Time per request: 1118.765 [ms] (mean)

Time per request: 0.559 [ms] (mean, across all concurrent requests)

Transfer rate: 488.82 [Kbytes/sec] received

Connection Times (ms)

min mean[+/-sd] median max

Connect: 0 56 216.7 1 1062

Processing: 4 71 161.9 24 670

Waiting: 4 70 161.9 24 670

Total: 16 127 271.1 26 1087

Percentage of the requests served within a certain time (ms)

50% 26

66% 26

75% 27

80% 57

90% 717

95% 727

98% 1085

99% 1086

100% 1087 (longest request)

看了以上关于怎么样使用nginx+Apache负载均衡及动静分离 ,如果大家还有什么地方需要了解的可以在行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,技术工程师在行业内拥有十几年的经验了。

0