nginx负载均衡一简单部署

一、介绍

1.负载均衡

  • 先简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上即可。

2.nginx的负载均衡

nginx 的 upstream目前支持 4 种方式的分配
1、轮询(默认)

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

2、weight

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

3、ip_hash

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

4、fair(第三方)

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

5、url_hash(第三方)

3.环境

三台虚拟机

  • Centos6.5 64位
    10.0.0.131 (部署负载均衡)
    10.0.0.130(已经部署好nginx)
    10.0.0.141(已经部署好nginx)

二、安装部署nginx负载均衡

1.关闭防火墙,SE

[root@nginx-proxy nginx-1.6.2]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@nginx-proxy nginx-1.6.2]# setenforce 0

2.下载nginx安装包

百度网盘下载:链接

[root@nginx-proxy ~]# ll nginx-1.6.2.tar.gz 
-rw-r--r--. 1 root root 804164 Aug  5 01:01 nginx-1.6.2.tar.gz

3.解压安装包

[root@nginx-proxy ~]# tar xf nginx-1.6.2.tar.gz 
[root@nginx-proxy ~]# cd nginx-1.6.2

4.创建nginx用户

[root@nginx-proxy nginx-1.6.2]# useradd nginx -s /sbin/nologin -M

5.安装依赖

[root@nginx-proxy nginx-1.6.2]# yum install gcc -y
[root@nginx-proxy nginx-1.6.2]# yum install pcre pcre-devel -y
[root@nginx-proxy nginx-1.6.2]# yum install openssl-devel -y

6.编译安装

[root@nginx-proxy nginx-1.6.2]# ./configure --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
[root@nginx-proxy nginx-1.6.2]# make && make install  
[root@nginx-proxy nginx-1.6.2]# mkdir /application
[root@nginx-proxy nginx-1.6.2]# ln -s /usr/local/nginx/ /application/nginx

7.设置环境变量

[root@nginx-proxy nginx-1.6.2]# cp /application/nginx/sbin/* /usr/local/sbin/

8.修改配置文件

官方负载均衡模块帮助:链接

[root@nginx-proxy nginx-1.6.2]# egrep -v "^$|#" /application/nginx/conf/nginx.conf.default > /application/nginx/conf/nginx.conf
[root@nginx-proxy nginx-1.6.2]# cat -n /application/nginx/conf/nginx.conf 
     1  worker_processes  1;
     2  events {
     3      worker_connections  1024;
     4  }
     5  http {
     6      include       mime.types;
     7      default_type  application/octet-stream;
     8      sendfile        on;
     9      keepalive_timeout  65;
    10      upstream backend {
    11          server 10.0.0.130:80       max_fails=3 fail_timeout=30s;
    12          server 10.0.0.141:80       max_fails=3 fail_timeout=30s;
    13
    14          }    
    15          server {
    16          listen       80;
    17          server_name  www.liangproxy.com;
    18          index  index.html index.htm;
    19          location / {
    20                  proxy_pass http://backend;
    21                  proxy_pass_header Host $host;
    22                  proxy_pass_header X-Forwarded-For $remote_addr;
    23          }
    24      }
    25  }

9.检查语法,启动服务

[root@nginx-proxy nginx-1.6.2]# 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@nginx-proxy nginx-1.6.2]# nginx 
[root@nginx-proxy nginx-1.6.2]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4965/nginx  

10.查看负载的两台虚拟机网页内容

[root@nginx-proxy nginx-1.6.2]# curl 10.0.0.130
10.0.0.130
[root@nginx-proxy nginx-1.6.2]# curl 10.0.0.141
10.0.0.141

11测试(成功的话,会一会显示130的机器一会显示141的机器内容)

[root@nginx-proxy ~]# for num in `seq 10`;do curl 10.0.0.131;done
10.0.0.141
10.0.0.130
10.0.0.141
10.0.0.130
10.0.0.141
10.0.0.130
10.0.0.141
10.0.0.130
10.0.0.141
10.0.0.130

猜你喜欢

转载自blog.csdn.net/liang_operations/article/details/81462105