Nginx负载均衡
2016-01-26 19:06
615 查看
Nginx负载均衡
在大型Web架构中分布式和负载均衡是必不可少的,一般企业常用的负载均衡方式有Nginx负载均衡、LVS负载均衡、硬件负载均衡器(贵)、cdn负载均衡(贵、大公司)等几种方式,LVS工作在网络4层架构中,而Nginx负载均衡工作在网络7层架构中,相对于性能方面来讲,LVS负载均衡方案要高出Nginx负载均衡方案不少,并且能够用在更多的场合,比如Mysql,Redis等的负载均衡上。Nginx负载均衡方案配置比较简单,并且只能应用在HTTP协议层的负载均衡上。废话不多说,先简单地讲解一下Nginx负载均衡的配置,解决中小型项目的燃眉之急。一、准备工作
1.安装准备(三台机器)没错,就是三台机器,最低要求,那不然玩什么负载均衡。觉得贵,没办法,只能自己装个虚拟机咯。推荐用VMware, 安装完Linux(我实验的时候是安装的Centos 6.5),然后安装Nginx(不太清楚的可以看一下我的这个Blog中Nginx安装配置部分),安装完成后,其余两台机器可以快速克隆。克隆后可能会出现eth0网卡找不到的情况(提示:修改网卡,具体自己百度吧)
2.配置明细
姑且,认为所有的基础环境已经搭建成功,并能够独立访问每台服务器。
下面,看一下配置明细:
之前我们准备了三台服务器分别为:
192.168.164.240 Master服务器
192.168.164.241 Slave-one 1号从服务器
192.168.164.242 Slave-two 2号从服务器
浏览器通过访问 Master服务器,然后Master服务器通过一定的规则来转发到两台从服务器上,从而实现负载均衡。
3.负载均衡配置
A.首先配置Master服务器
#user nobody; worker_processes 4; pid /var/run/nginx.pid; worker_rlimit_nofile 65535; events { use epoll; worker_connections 10000; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 0; #gzip on; # upstream slave config upstream master{ server 192.168.164.241 weight=2; server 192.168.164.242 weight=1; } # upstream master server server{ listen 80; server_name master; location ~ / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffering off; proxy_pass http://master; } } }
upstream 是上游的意思,也就是来源。当一个请求到来的时候,location到了 / ,然后通过 proxy_pass http://master; 通过上面配置的的upstream来进行代理,请求被转发到upstream上,upstream会按照一定的权重规则(weight=1,默认的权重是1:1,就是平均分配。但是如果在开发过程中如果某台服务器相对来说比较破旧,就需要将它的权重值调低,在upstream中值越大,权重越高,被调用的频率也就会越高。比如客户端发来3次请求,将会有两次落在slave-one服务器上,一次落在slave-two服务器上,这就是权重的作用。)
B.配置两台从服务器
#user nobody; worker_processes 4; pid /var/run/nginx.pid; worker_rlimit_nofile 65535; events { use epoll; worker_connections 10000; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 0; #gzip on; # slave server config server { listen 80; server_name localhost; #charset koi8-r; access_log logs/host.access.log main; location ~ / { root /website; index index.html index.htm; } } }
4.测试
搭建好环境后,分别在Slave-one服务器 和 Slave-two服务器 的/website/index.html中写上不同的内容。
# 向Slave-one服务器的index.html文件写入 echo 'I am from 241 Server' > /website/index.html # 向Slave-two服务器的index.html文件写入 echo 'I am from 242 Server' > /website/index.html
通过访问 http://192.168.164.240/,我们可以看到,发送到Master服务器的请求被成功转发到Slave服务器上,并且Slave-one和Slave-two服务器之间的频率是2:1。
5.Slave故障
之前我也以为,Master服务器对Slave服务器的down是毫不知情的,但是经过我测试,我在访问Master服务器的时候,突然stop掉其中一台Slave服务器,结果Nginx能够正常识别到Slave的挂掉,并且以后的请求都不会再发送到挂掉的那台服务器。
其实,这里Master服务器还是存在单点故障,放下次Blog中讲解吧。
相关文章推荐
- Nginx使用ngx_zeromq模块返回502错误的解决方法
- WordPress程序伪静态规则(Nginx/Apache)及二级目录规则
- Nginx+Lua开发入门
- nginx高可用负载均衡配置
- Nginx安装 与使用 转载
- nginx安装与使用
- zabbix管理七之监控nginx性能
- DJANGO技巧两则:模拟MKDIR -P及配合NGINX上传大文件不使超时
- Nginx配置文件详细说明
- flume nginx 日志处理异常 JsonParseException: Unexpected character ('(' (code 40)): expected a valid value
- nginx 配置其他路径
- nginx connection failed due to Permission denied on CentOS7
- nginx启动、重启、关闭
- 最佳Nginx日志分析工具Goaccess
- nginx自动切割访问日志
- Nginx + Apache 公用 80 端口的配置方案。
- nginx日志自动按天分隔
- 主备切换,双机热备,负载均衡,nginx
- 从源码角度理解nginx和uwsgi的通信过程
- nginx_lua 扩展让 nginx 拥有可编程能力