共计 5683 个字符,预计需要花费 15 分钟才能阅读完成。
Nginx 负载均衡器的优点:
- 实现看弹性化操作的架构,压力增大的时候可以临时添加后端 Web 服务器;
- upstream 具有负载均衡能力(默认使用轮询),可以自动判断下面的机器,并且自动踢出不能正常提供服务的机器;
- Keepalvied 加 Nginx 监测脚本可保证单个 nginx 负载均衡器的有效性, 避免单点故障
系统
两台 Nginx:
CentOS6.7 x86_64
两台 Web:
Ubuntu 15.04 desktop
拓扑
IP 地址
nginx(主 LB):192.168.15.132
nginx(备 LB):192.168.15.133
VIP 地址:192.168.15.135
Real1 的 IP:192.168.15.128
Real2 的 IP:192.168.15.130
部署整个环境用到的软件为:
nginx-1.6.3.tar.gz
prce-8.38.tar.gz
zlib-1.2.8.tar.gz
①2 台 Web 主机(Ubuntu)上部署 Nginx+PHP-FPM+MySQL,此处省略。
②分别在二台 Nginx 负载均衡器上安装 Nginx,配置
安装 GCC 编译器等工具:
yum install -y gcc gcc-c++ autoconf automake libtool make openssl openssl-devel
安装 Nginx:
wget http://exim.mirror.fr/pcre/pcre-8.38.tar.gz
tar -zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure
make && make install
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install
wget http://nginx.org/download/nginx-1.6.3.tar.gz
tar -zxvf nginx-1.6.3.tar.gz
cd nginx-1.6.3/
./configure –prefix=/usr/local/nginx
–sbin-path=/usr/local/nginx/sbin/nginx
–conf-path=/usr/local/nginx/conf/nginx.conf
–pid-path=/usr/local/nginx/logs/nginx.pid \
–with-http_ssl_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \
make && make install
注:查询 ”./configure –help” 相关模块,按需求指定启用
Nginx.conf 配置文件,二个 nginx 负载均衡器的文件一样
user www-data www-data;
worker_processes 1;
error_log /usr/local/nginx/logs/error.log notice;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 51200;
}
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;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
server_tokens off;
keepalive_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
upstream backend
{
server 192.168.15.128;
server 192.168.15.130;
}
server {
listen 80;
server_name 192.168.15.135;
location / {
root html;
index index.php index.html index.htm;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#后端的 Web 服务器可以通过 X -Forwarded-For 获取用户真实 IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /nginx_status {
stub_status on;
auth_basic “NginxStatus”;
auth_basic_user_file /usr/local/nginx/htpasswd;
#allow 127.0.0.1;
#deny all;
}
location ~* \.(ini|docx|txt|doc|pdf)$ {
#禁止访问文档性文件
root /usr/share/nginx/html;
deny all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|html|htm|css)$ {
root /home/image;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /home/image;
if (!-e $request_filename) {
proxy_pass http://backend;
}
}
}
}
③在二台 Nginx 上安装及配置 keepalived:
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar -zxvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure –sysconf=/etc/ –with-kernel-dir=/usr/src/kernels/2.6.32-573.8.1.el6.x86_64
make && make install
ln -s /usr/local/sbin/keepalived /sbin/
# 这一步很重要,不执行 ln - s 会报错“Starting keepalived: /bin/bash: keepalived: command not found”
service keepalived start
二台 Nginx 上 keepalived.conf 配置文件如下,配置完成后分别 service keepalived start 启动。检验 keepalived 配置是否成功
主:
global_defs {
notification_email {
test@163.com
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
}
vrrp_script chk_http_port {
script “/usr/local/src/check_nginx_pid.sh”
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface bond0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.15.135
}
}
备:
global_defs {
notification_email {
test@163.com
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_BACKUP
}
vrrp_script chk_http_port {
script “/usr/local/src/check_nginx_pid.sh”
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface bond0
virtual_router_id 51
priority 66
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.15.135
}
}
以下是针对 nginx 状态进行检测的脚本,第一次 nginx 服务死掉时,会重新启动,如果 Nginx 服务无法正常启动,则杀掉 keepalived 进程
vim /usr/local/src/check_nginx_pid.sh
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [$A -eq 0];then
/usr/local/nginx/sbin/nginx
if [`ps -C nginx –no-header |wc -l` -eq 0];then
killall keepalived
fi
fi
Ok,开始 nginx 负载均衡测试,停掉其中一台的任何服务,不影响整个系统的运作。
注:两台 LBServer 也可分别添加一个 VIP①②(Keepalived 心跳监控,服务不可用或者宕机,VIP①被备 LBServer 接管),外部使用智能 DNS 轮询两个 VIP①②,提高硬件资源利用率。
更多 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 环境下 Nginx 实现 3 台虚拟机负载均衡 http://www.linuxidc.com/Linux/2015-12/125875.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-12/126865.htm