Nginx负载均衡

来源:互联网 发布:mac 如何卸载云梯 编辑:程序博客网 时间:2024/05/16 12:49

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中讲解吧。

0 0
原创粉丝点击