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

LB负载均衡集群分两类LVS (四层)和 nginx或haproxy /haˈprɒksi/ (七层)

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

现在实现以下功能,拓扑图:
在这里插入图片描述
在这里插入图片描述

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

源码编译安装nginx
一、安装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功能

安装nginx:
[root@xuegod63 ~]# ll nginx-1.8.0.tar.gz -h #整个nginx文件不到只813K,很小

-rw-r--r-- 1 root root 813K Jul 14 20:17 nginx-1.8.0.tar.gz 

[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 ~]# ./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@xuegod63 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)
[root@xuegod63 ~]#make -j 4
[root@xuegod63 ~]#make install

生成运行nginx的用户:
[root@xuegod63 nginx-1.8.0]# useradd -u 8000 -s /sbin/nologin nginx
[root@xuegod63 nginx-1.8.0]# id !$

id nginx
uid=8000(nginx) gid=8000(nginx) groups=8000(nginx)

nginx主要目录结构:
[root@xuegod63 /]# ls /server/nginx-1.8.0/

conf  html  logs  sbin
conf  #配置文件
html  #网站根目录
logs  #日志
sbin  #nginx启动脚本

主配置文件
[root@xuegod63 /]# ls /server/nginx-1.8.0/conf/nginx.conf

启动nginx:
[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx
[root@xuegod63 /]# netstat -antup | grep :80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      5281/httpd        
[root@xuegod63 /]# netstat -antup | grep :80

开机启动:
[root@xuegod63 nginx-1.8.0]# echo '/server/nginx-1.8.0/sbin/nginx & ' >> /etc/rc.local
测试:
http://192.168.1.63/

nginx服务日常操作:
测试配置文件语法:
[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

重新加载配置文件
[root@xuegod63 nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -s reload
关闭nginx
[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s stop

配置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 / { 。。。} 中添加以下内容  #定义分发策略
location / {
            root   html;
            index  index.html index.htm;
     
        if ($request_uri ~* \.html$){
                   proxy_pass http://htmlservers;
           }   
        if ($request_uri ~* \.php$){
                   proxy_pass http://phpservers;
           }   
                   proxy_pass http://picservers;

      }

如图:

在这里插入图片描述
把以下内容注释掉,否则php文件直接在nginx服务器上解析了,不再解析给后端服务器:

#       location ~ \.php$ {
 73 #           root           html;
 74 #           fastcgi_pass   127.0.0.1:9000;
 75 #           fastcgi_index  index.php;
 76 #           fastcgi_param  SCRIPT_FILENAME  /server/nginx-1.8.0/html$fastcgi_script_name;
 77 #           include        fastcgi_params;
 78 #       }

在这里插入图片描述
#定义负载均衡设备的 Ip
#定义负载均衡设备的 Ip

在配置文件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地址
在这里插入图片描述
重新加载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

配置后端服务器: xuegod62
配置web服务器:
[root@xuegod62 html]# yum install httpd php -y
生成静态测试文件:
root@xuegod62 html]#echo 192.168.1.62 > /var/www/html/index.html
生成动态测试文件:
[root@xuegod62 html]#vim /var/www/html/test.php #写如以下内容:

192.168.1.62-php
<?php
phpinfo();
?>

生成图片文件:
上传如下图片,到“xuegod62网站/var/www/html/目录下:
在这里插入图片描述

启动apache服务器:
[root@xuegod62 html]# service httpd restart

配置后端服务器: xuegod64

IP: 192.168.1.64 

配置web服务器:
[root@xuegod64 html]# yum install httpd php -y
生成静态测试文件:

echo 192.168.1.64 > /var/www/html/index.html

生成动态测试文件:
vim /var/www/html/test.php #写如以下内容:

192.168.1.64-php
<?php
phpinfo();
?>

生成图片文件:
上传如下图片,到“xuegod64网站/var/www/html/目录下:
在这里插入图片描述
[root@xuegod64 html]# service httpd restart

到此nginx实现负载均衡结束。
在这里插入图片描述
在这里插入图片描述

测试性能:apache-Toosl

扩展: 文件打开数过多
[root@xuegod64 html]# ab -n 1000 -c 1000 http://192.168.1.62/index.html #运行正常
[root@xuegod64 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文件太多。

#ulimit -a #查看

ulimit -n
1024

系统默认一个进程最多同时允许打开1024的文件
解决:
#ulimit -n 10240 #报错的解决方法

Nginx负载的5种策略设置方法:

1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream backserver {
server 192.168.1.62;
server 192.168.1.64;
}

2、指定权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream backserver {
server 192.168.1.62 weight=1;
server 192.168.1.64 weight=2;
}

3、IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream backserver {
ip_hash;
server 192.168.1.62:80;
server 192.168.1.64:80;
}

4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backserver {
server server1;
server server2;
fair;
}

5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

总结,扩展:
如有tomcat ,apache,squid 配置为如下:
[root@xuegod63 conf]# vim nginx.conf # 在最后添加以下内容。 定义服务器组

upstream  tomcat_servers {
        server  192.168.1.2:8080;
        server  192.168.1.1:8080;
        server  192.168.1.11:8080;
}
upstream  apache_servers {
        server  192.168.1.5:80;
        server  192.168.1.177:80;
        server  192.168.1.15:80;
}
upstream  squid_servers {
        server  192.168.1.26:3128;
        server  192.168.1.55:3128;
        server  192.168.1.18:3128;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要使用Nginx实现动静分离负载均衡集群,可以按照以下步骤进行操作: 1. 安装Nginx:首先确保您的服务器上已经安装了Nginx。您可以从Nginx官方网站上下载并按照指示进行安装。 2. 配置Nginx:编辑Nginx的配置文件,通常位于`/etc/nginx/nginx.conf`或`/etc/nginx/conf.d/default.conf`路径下。以下是一个简单的配置示例: ``` http { upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } location /static { alias /var/www/static; } } } ``` 在上述示例中,`upstream`指令定义了后端服务器的列表。您可以将实际的后端服务器添加到其中。`location /`指令将所有动态请求代理到后端服务器集群,而`location /static`则指定了静态文件存放的路径。 3. 配置后端服务器:确保您的后端服务器已经准备好,并且可以处理来自Nginx的动态请求。 4. 测试:完成配置后,重新加载Nginx配置文件并启动Nginx服务。然后,使用浏览器访问您的域名并测试是否成功实现动静分离负载均衡集群。 请注意,以上步骤仅为简单示例,实际配置可能因具体需求而有所不同。您可能还需要考虑安全性、缓存设置、SSL证书等其他方面的配置。同时,确保您的服务器能够处理预期的负载,并根据需要进行横向扩展。 希望对您有所帮助!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值