Nginx反向代理负载均衡群集,nginx负载均衡群集


Nginx反向代理负载均衡群集实战

使用Nginx做负载均衡器,代理web服务器,用户请求的数据都指向Nginx负载均衡器,Nginx负责调度后端的Web服务器提供服务。

环境搭建及说明:

nginx负载均衡器LNMP环境或只安装nginx服务;两块网卡,192.168.11.30(模拟公网ip),192.168.20.30(内网)

web服务器LAMP环境1:ip地址为内网 192.168.20.10 apache为2.4版本

web服务器LAMP环境2:ip地址为内网 192.168.20.11

web服务器LAMP环境3:ip地址为内网 192.168.20.12

三台web服务器网站目录,程序保持一致。内网ip保持与nginx负载均衡器同一个网段。

1、只有一个站点的配置:

web1、web2、web3上操作:

httpd配置文件加入,允许网站目录访问;开启vhost虚拟配置文件。

# vi /etc/httpd/httpd.conf

Include /etc/httpd/extra/httpd-vhosts.conf

Options FollowSymLinks

AllowOverride none

Require all granted

虚拟主机配置

# vi /etc/httpd/extra/httpd-vhosts.conf

DocumentRoot "/data/www"

ServerName www.linuxboy.net

创建目录,写入index.html文件区分。

mkdir /data/www

在每一个web目录下写入index.html,内容分别为:This is LAMP 1 !;This is LAMP 2!;This is LAMP 3!

# curl 192.168.20.10

This is LAMP 1 !

# curl 192.168.20.11

This is LAMP 2!

# curl 192.168.20.12

This is LAMP 3!

nginx代理服务器操作:

写一个代理配置文件:

# cat /usr/local/nginx/conf/vhosts/nginxproxy.conf

upstream backend {

server 192.168.20.10:80 weight=1 max_fails=3 fail_timeout=30s;

server 192.168.20.11:80 weight=1 max_fails=3 fail_timeout=30s;

server 192.168.20.12:80 weight=1 max_fails=3 fail_timeout=30s;

}

server {

listen 80;

server_name www.linuxboy.net;

index index.html;

location / {

proxy_pass https://backend;

}

}

hosts添加本地ip地址解析,模拟公网ip对应域名;windows本地hosts也要增加解析;

# cat /etc/hosts

192.168.11.30 www.linuxboy.net


使用curl测试,默认rr轮询,访问一次web1,一次web2,一次web3

使用for循环执行,查看访问结果:

# for n in `seq 10`;do curl www.linuxboy.net;sleep 2;done

This is LAMP 2!

This is LAMP 1 !

This is LAMP 3!

This is LAMP 2!

This is LAMP 1 !

This is LAMP 3!

This is LAMP 2!

This is LAMP 1 !

This is LAMP 3!

This is LAMP 2!

2、多个站点的配置:


在web1,web2,web3,增加第2个站点 bbs.linuxboy.net

httpd配置文件加入,允许网站目录访问;

Options FollowSymLinks

AllowOverride none

Require all granted

虚拟主机配置增加

# vi /etc/httpd/extra/httpd-vhosts.conf

DocumentRoot "/data/bbs"

ServerName bbs.linuxboy.net

创建目录,写入index.html文件区分。

mkdir /data/bbs

在每一个web目录下写入index.html,内容分别为:This is BBS.linuxboy.net 1!;This is BBS.linuxboy.net 2!;This is BBS.linuxboy.net 3!

nginx负载均衡服务器,虚拟主机增加server:

server {

listen 80;

server_name bbs.linuxboy.net;

index index.html;

location / {

proxy_pass https://backend;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

hosts添加本地ip地址解析,模拟公网ip对应域名;windows本地hosts也要增加解析;

# cat /etc/hosts

192.168.11.30 www.linuxboy.net bbs.linuxboy.net

使用for循环执行,查看访问结果:

# for n in `seq 10`;do curl bbs.linuxboy.net;sleep 2;done

This is BBS.linuxboy.net 1!

This is BBS.linuxboy.net 2!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 1!

This is BBS.linuxboy.net 2!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 1!

This is BBS.linuxboy.net 2!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 1!

3、upstream 下面增加ip_hash;

测试结果如下:保持用户连接,也会导致分配不均。

# for n in `seq 10`;do curl bbs.linuxboy.net;sleep 2;done

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

4、增加一个upstream,单独针对bbs的请求进行负载均衡,并设置权重,配置文件如下:

upstream backend {

server 192.168.20.10:80 weight=2 max_fails=3 fail_timeout=30s;

server 192.168.20.11:80 weight=1 max_fails=3 fail_timeout=30s;

}

upstream bbs {

server 192.168.20.11:80 weight=1 max_fails=3 fail_timeout=30s;

server 192.168.20.12:80 weight=2 max_fails=3 fail_timeout=30s;

}

server {

listen 80;

server_name www.linuxboy.net;

index index.html;

location / {

proxy_pass https://backend;

}

}

server {

listen 80;

server_name bbs.linuxboy.net;

index index.html;

location / {

proxy_pass https://bbs;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

实验结果如下

# for n in `seq 10`;do curl bbs.linuxboy.net;sleep 2;done

This is BBS.linuxboy.net 2!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 2!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 2!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 3!

This is BBS.linuxboy.net 2!

# for n in `seq 10`;do curl www.linuxboy.net;sleep 2;done

This is LAMP 2!

This is LAMP 1 !

This is LAMP 1 !

This is LAMP 2!

This is LAMP 1 !

This is LAMP 1 !

This is LAMP 2!

This is LAMP 1 !

This is LAMP 1 !

This is LAMP 2!

相关内容