没有合适的资源?快使用搜索试试~ 我知道了~
首页使用nginx-haproxy实现七层负载均衡笔记
资源详情
资源评论
资源推荐

www.eimhe.com 美河学习在线收集分享
学神 IT 教育,祝您早日成为技术牛人! Man 老师
使用 nginx haproxy 实现七层负载均衡
内容:
• 实战:使用 nginx 实现动静分离的负载均衡集群
• 实战:使用 haproxy 实现负载均衡集群
LB 负载均衡集群分两类: LVS (四层)和 nginx 或 haproxy (七层)
客户端通过访问分发器的 VIP 来访问网站
|
现在应用更复杂,比如现在网站页面有: .php .html .png .jpeg .jsp 等, 有动态页面有静
态页面。静态页面一般是不变的,想访问更快些,前面学习过 SQUID。
|
但是前面的 LVS 是四层的。基于 IP 的。现在需要在应用层基于不同的应用进行分发。
|
七层 LB , Nginx / Haproxy 都可以支持 7 层 LB
现在实现以下功能,拓扑图:
工作中,希望这样:
静态文件处理:可以使用 nginx 或 apache
动文件处理: apache ,tomcat
图片文件处理: squid
使用 nginx 实现动静分离的负载均衡集群
1. Nginx 负载均衡基础知识
Nginx 的 upstream 负载的 5 种方式,目前最常用 前 3 种方式
1)、轮询(默认)

www.eimhe.com 美河学习在线收集分享
学神 IT 教育,祝您早日成为技术牛人! Man 老师
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
2)、weight
指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。
3)、ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的
问题。
4)、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
5)、url_hash(第三方) url 哈西
按访问 url 的 hash 结果来分配请求,使同样的 url 定向到同一个后端服务器,后端服务器为缓存时比较
有效
实例 1:使用 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 响应中
的一些文本)

www.eimhe.com 美河学习在线收集分享
学神 IT 教育,祝您早日成为技术牛人! Man 老师
--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/

www.eimhe.com 美河学习在线收集分享
学神 IT 教育,祝您早日成为技术牛人! Man 老师
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
[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s start #没有 start 参数
nginx: invalid option: "-s start"
配置 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;
剩余18页未读,继续阅读

















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0