共计 4729 个字符,预计需要花费 12 分钟才能阅读完成。
使用 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
<Directory /data/www>
Options FollowSymLinks
AllowOverride none
Require all granted
</Directory>
虚拟主机配置
# vi /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot “/data/www”
ServerName www.linuxidc.com
</VirtualHost>
创建目录,写入 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.linuxidc.com;
index index.html;
location / {
proxy_pass http://backend;
}
}
hosts 添加本地 ip 地址解析,模拟公网 ip 对应域名;windows 本地 hosts 也要增加解析;
# cat /etc/hosts
192.168.11.30 www.linuxidc.com
使用 curl 测试,默认 rr 轮询,访问一次 web1,一次 web2,一次 web3
使用 for 循环执行,查看访问结果:
# for n in `seq 10`;do curl www.linuxidc.com;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.linuxidc.com
httpd 配置文件加入,允许网站目录访问;
<Directory /data/bbs>
Options FollowSymLinks
AllowOverride none
Require all granted
</Directory>
虚拟主机配置增加
# vi /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot “/data/bbs”
ServerName bbs.linuxidc.com
</VirtualHost>
创建目录,写入 index.html 文件区分。
mkdir /data/bbs
在每一个 web 目录下写入 index.html,内容分别为:This is BBS.linuxidc.com 1!;This is BBS.linuxidc.com 2!;This is BBS.linuxidc.com 3!
nginx 负载均衡服务器,虚拟主机增加 server:
server {
listen 80;
server_name bbs.linuxidc.com;
index index.html;
location / {
proxy_pass http://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.linuxidc.com bbs.linuxidc.com
使用 for 循环执行,查看访问结果:
# for n in `seq 10`;do curl bbs.linuxidc.com;sleep 2;done
This is BBS.linuxidc.com 1!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 1!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 1!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 1!
3、upstream 下面增加 ip_hash;
测试结果如下:保持用户连接,也会导致分配不均。
# for n in `seq 10`;do curl bbs.linuxidc.com;sleep 2;done
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 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.linuxidc.com;
index index.html;
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name bbs.linuxidc.com;
index index.html;
location / {
proxy_pass http://bbs;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
实验结果如下
# for n in `seq 10`;do curl bbs.linuxidc.com;sleep 2;done
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 2!
# for n in `seq 10`;do curl www.linuxidc.com;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!
同理,如果有多台服务器,多个站点可以进行分配、关联对应。
更多 Nginx 相关教程见以下内容 :
CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm
使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm
搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm
CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm
CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm
CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm
Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm
Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm
Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-08/122111.htm