nginx实现动静分离的负载均衡集群

网友投稿 940 2022-11-06

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

nginx实现动静分离的负载均衡集群

1、负载均衡介绍

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

客户端通过访问分发器的VIP来访问网站,现在应用更复杂,比如现在网站页面有:.php  .html    .png    .jpeg  .jsp 等, 有动态页面有静态页面。静态页面一般是不变的,想访问更快些。但是前面的LVS是四层的。基于IP的。现在需要在应用层基于不同的应用进行分发。Nginx Haproxy都可以支持7层

工作中,希望这样:

静态文件处理:可以使用nginx 或apache

动文件处理: apache ,tomcat

图片文件处理: squid

2、 Nginx 负载均衡基础知识

Nginx 的 upstream 负载的5种方式,目前最常用 前3种方式

1)、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。

2)、weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

3)、ip_hash

每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。

4)、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

5)、url_hash(第三方) url哈西

按访问url的hash结果来分配请求,使同样的url定向到同一个后端服务器,后端服务器为缓存时比较有效

3、使用nginx实现负载均衡和动静分离

3.1安装nginx时必须先安装相应的编译工具和相关依赖

[root@docker-02 ~]# yum -y install gcc gcc-c++ autoconf automake [root@docker-02 ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

zlib:nginx提供gzip模块,需要zlib库支持openssl:nginx提供ssl功能pcre:支持地址重写rewrite功能

3.2安装nginx

[root@docker-02 ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz [root@docker-02 ~]# tar -zxvf nginx-1.8.0.tar.gz -C usr/local/src/ [root@docker-02 nginx-1.8.0]# cd usr/local/src/nginx-1.8.0/ [root@docker-02 nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --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 [root@docker-02 nginx-1.8.0]# make -j 4  [root@docker-02 nginx-1.8.0]# make install ##查看参数: [root@docker-02 nginx-1.8.0]# ./configure --help | grep mp4

参数:

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

--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文件支持(提供寻求内存使用基于时间的偏移量文件)

编译和安装: (查看CPU逻辑数cat proc/cpuinfo | grep processor | wc -l)

3.3生成运行nginx的用户

[root@docker-02 nginx-1.8.0]# useradd -u 8000 -s sbin/nologin nginx [root@docker-02 nginx-1.8.0]# id !$ id nginx uid=8000(nginx) gid=8000(nginx) 组=8000(nginx)

3.4介绍nginx主要目录结构

[root@docker-02 nginx]# ls usr/local/nginx/ conf html logs sbin conf  #配置文件 html  #网站根目录 logs  #日志 sbin  #nginx启动脚本 ##主配置文件: [root@docker-02 nginx]# ls usr/local/nginx/conf/nginx.conf

3.5开机启动nginx

[root@docker-02 nginx]# echo '/usr/local/nginx/sbin/nginx &' >> etc/rc.local  ##检查 [root@docker-02 sbin]# ./nginx -t nginx: the configuration file usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file usr/local/nginx/conf/nginx.conf test is successful ##启动报错 [root@docker-02 sbin]# ./nginx -s reload nginx: [error] invalid PID number ""in"/usr/local/nginx/logs/nginx.pid" ##解决办法 [root@docker-02 sbin]# usr/local/nginx/sbin/nginx -c usr/local/nginx/conf/nginx.conf ##测试 [root@docker-02 sbin]# curl 172.17.1.151

4、配置nginx成为分发器,实现动静分离

[root@docker-02 sbin]# cd usr/local/nginx/conf/ ##配置文件目录 [root@docker-02 conf]# cp nginx.conf nginx.conf.bak ##备份一下配置文件 [root@docker-02 conf]# vim nginx.conf 改:# user nobody;   ##指定启动nginx用户 为:user nginx nginx;  改: 41        #access_log logs/host.access.log main; 42 43        location { 44            root   html; 45            index index.html index.htm; 46        if($request_uri~* \.html$){ 47                  proxy_pass http://htmlservers; 48          } 49        if($request_uri~* \.php$){ 50                  proxy_pass http://phpservers; 51          } 52                  proxy_pass http://picservers; 53        } ##把以下内容注释掉,否则php文件直接在nginx服务器上解析了,不再解析给后端服务器: 70        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 71        # 72        #location ~ \.php$ { 73        #   root           html; 74        #   fastcgi_pass   127.0.0.1:9000; 75        #   fastcgi_index index.php; 76        #   fastcgi_param SCRIPT_FILENAME usr/local/nginx/html$fastcgi_s   cript_name; 77        #   include       fastcgi_params; 78        #} ##定义负载均衡设备的 Ip ##定义负载均衡设备的 Ip ###在配置文件nginx.conf的最后一行}前,添加以下内容: 123upstream htmlservers {   #定义负载均衡服务器组名称 124        server 172.17.1.150:80; 125        server 172.17.1.152:80; 126} 127upstream phpservers{ 128        server 172.17.1.150:80; 129        server 172.17.1.152:80; 130} 131upstream picservers { 132        server 172.17.1.150:80; 133        server 172.17.1.152:80; 134} #后期工作中,根据工作中的需要,配置成具体业务的IP地址

5、重新加载nginx服务器配置文件

[root@docker-02 conf]# ./nginx -t [root@docker-02 conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

6、配置后端服务器: 172.17.1.150 172.17.1.151(两台机器同时执行)

[root@docker-01 html]# yum install httpd php -y ##另一台写maoxiaopu进配置文件 [root@docker-01 html]# echo yunweimao > /var/www/html/index.html [root@docker-01 html]# vim /var/www/html/test.php 172.17.1.150-php 

上传一张图片,到172.17.1.150网站/var/www/html/目录下:

启动apache服务器:

[root@docker-01 html]# service httpd restart

7、测试

7.1测试静态界面http://172.17.1.151/

7.2测试转发动态页面

7.3测试转发图片

女票从事平面设计这一块,需要设计海报,宣传册的也可以联系我,明码标价3元/1页。

上一篇:软件测试培训之实现编码评审
下一篇:软件测试培训之执行白盒测试
相关文章

 发表评论

暂时没有评论,来抢沙发吧~