nginx下带健康检测的负载均衡
2012-09-14 11:06
190 查看
因为nginx自带的fail次数检测并不能真正地实现停止对后端app的转发,因此考虑用nginx_upstream_check_module模块来做后端web的健康监测
nginx_upstream_check_module的下载地址: https://github.com/yaoweibin/nginx_upstream_ href="http://www.linuxyan.com/tag/check_module" target=_blank>check_module
附件里我也传了最新的模块。
先说一下实验环境:
前端:nginx负责均衡服务器 192.168.0.188
后端:一台apache服务器 192.168.0.170 打开网页的页面是:0.170
一台IIS服务器 192.168.0.140 打开网页的页面是:0.140
一、配置nginx服务器:
1.安装基本的软件,更新系统时间:
yum -y install gcc gcc-c++ pcre-devel ntp
ntpdate time.windows.com
2.建立nginx运行的账户:nginx
groupadd nginx
useradd -g nginx nginx
3.我们安装的是nginx-1.2.1的版本,下载地址为:
http://www.nginx.org/download/nginx-1.2.1.tar.gz
把nginx和模块的安装包放到/opt下
解压缩nginx软件包:tar -zxvf nginx-1.2.1.tar.gz
然后把模块的安装包改名为health:
unzip yaoweibin-nginx_upstream_check_module-v0.1.6-29-g2c5a2c8.zip
mv yaoweibin-nginx_upstream_check_module-v0.1.6-29-g2c5a2c8 health
把模块补丁到nginx里:
cd nginx-1.2.1
path -p1 < /opt/health/check_1.2.1.patch
(注:health里有3个特殊的文件,如果你的nginx版本在1.2.2以上的请用check_1.2.2+.patch,如果是1.2.1版本以下的用check.patch,如果是1.2.1版本的用check_1.2.1.patch)
编译安装:
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--add-module=/opt/health
make && make install
4.配置nginx.conf文件,在http下添加:
upstream backend
{
server 192.168.0.170;
check interval=1000 rise=2 fall=2 timeout=1000;
server 192.168.0.140;
check interval=1000 rise=2 fall=2 timeout=1000;
}
注:interval检测间隔时间,单位为毫秒,rsie请求2次正常的话,标记此realserver的状态为up,fall表示请求2次都失败的情况下,标记此realserver的状态为down,timeout为超时时间,单位为毫秒
在server字段配置反向代理和健康检测的页面:
server {
listen 80;
server_name 192.168.0.188;
#charset koi8-r;
#access_log logs/host.access.log main;
#下面为添加的
location / {
proxy_pass http://backend;
}
location /nstatus {
check_status;
access_log off;
#allow SOME.IP.ADD.RESS;
#deny all;
}
保存退出.
建立nginx运行需要的目录:
mkdir -p /var/tmp/nginx
建立nginx启动服务:
vim /etc/init.d/nginx
在里面添加内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx" prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" |
sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
保存退出,然后给执行权限:
chmod +x /etc/init.d/nginx
添加到服务:
chkconfig --add nginx
chkconfig nginx on
运行nginx
service nginx start
访问192.168.0.188会看到
看到已经开始负载了,是轮询的
我们看一下检测界面:
我们把192.168.0.170关掉,等2s看一下:
再次访问192.168.0.188只能看到0.140了。
本文出自 “linux开源-不断的总结....” 博客,请务必保留此出处http://fantefei.blog.51cto.com/2229719/990521
nginx_upstream_check_module的下载地址: https://github.com/yaoweibin/nginx_upstream_ href="http://www.linuxyan.com/tag/check_module" target=_blank>check_module
附件里我也传了最新的模块。
先说一下实验环境:
前端:nginx负责均衡服务器 192.168.0.188
后端:一台apache服务器 192.168.0.170 打开网页的页面是:0.170
一台IIS服务器 192.168.0.140 打开网页的页面是:0.140
一、配置nginx服务器:
1.安装基本的软件,更新系统时间:
yum -y install gcc gcc-c++ pcre-devel ntp
ntpdate time.windows.com
2.建立nginx运行的账户:nginx
groupadd nginx
useradd -g nginx nginx
3.我们安装的是nginx-1.2.1的版本,下载地址为:
http://www.nginx.org/download/nginx-1.2.1.tar.gz
把nginx和模块的安装包放到/opt下
解压缩nginx软件包:tar -zxvf nginx-1.2.1.tar.gz
然后把模块的安装包改名为health:
unzip yaoweibin-nginx_upstream_check_module-v0.1.6-29-g2c5a2c8.zip
mv yaoweibin-nginx_upstream_check_module-v0.1.6-29-g2c5a2c8 health
把模块补丁到nginx里:
cd nginx-1.2.1
path -p1 < /opt/health/check_1.2.1.patch
(注:health里有3个特殊的文件,如果你的nginx版本在1.2.2以上的请用check_1.2.2+.patch,如果是1.2.1版本以下的用check.patch,如果是1.2.1版本的用check_1.2.1.patch)
编译安装:
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--add-module=/opt/health
make && make install
4.配置nginx.conf文件,在http下添加:
upstream backend
{
server 192.168.0.170;
check interval=1000 rise=2 fall=2 timeout=1000;
server 192.168.0.140;
check interval=1000 rise=2 fall=2 timeout=1000;
}
注:interval检测间隔时间,单位为毫秒,rsie请求2次正常的话,标记此realserver的状态为up,fall表示请求2次都失败的情况下,标记此realserver的状态为down,timeout为超时时间,单位为毫秒
在server字段配置反向代理和健康检测的页面:
server {
listen 80;
server_name 192.168.0.188;
#charset koi8-r;
#access_log logs/host.access.log main;
#下面为添加的
location / {
proxy_pass http://backend;
}
location /nstatus {
check_status;
access_log off;
#allow SOME.IP.ADD.RESS;
#deny all;
}
保存退出.
建立nginx运行需要的目录:
mkdir -p /var/tmp/nginx
建立nginx启动服务:
vim /etc/init.d/nginx
在里面添加内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx" prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" |
sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
保存退出,然后给执行权限:
chmod +x /etc/init.d/nginx
添加到服务:
chkconfig --add nginx
chkconfig nginx on
运行nginx
service nginx start
访问192.168.0.188会看到
看到已经开始负载了,是轮询的
我们看一下检测界面:
我们把192.168.0.170关掉,等2s看一下:
再次访问192.168.0.188只能看到0.140了。
本文出自 “linux开源-不断的总结....” 博客,请务必保留此出处http://fantefei.blog.51cto.com/2229719/990521
相关文章推荐
- nginx反向代理并开启缓存+url重写+带健康检测的负载均衡
- Nginx实现反向代理,负载均衡,url重写以及健康检测
- Nginx实现反向代理,负载均衡,url重写以及健康检测
- Nginx 负载均衡 后端 监控检测 nginx_upstream_check_module 模块的使用
- 【nginx】负载均衡和proxy的配置--后端检测扩展
- nginx实现反向代理+健康检测
- nginx后端节点的健康检测
- Nginx 负载均衡 后端 监控检测 nginx_upstream_check_module 模块的使用
- Nginx负载均衡+监控状态检测
- nginx后端节点的健康检测
- Nginx Tengine ngx_http_upstream_check_module 健康功能检测使用
- 负载均衡VIP /LVS 的健康检测方式
- NGINX 健康检查和负载均衡机制分析
- nginx 健康检查和负载均衡机制分析
- NGINX 健康检查和负载均衡机制分析
- nginx(六)反向代理(proxy)与负载均衡(upstream)以及健康状态监测。
- nginx自动检测后台服务器健康状态
- nginx 健康检查和负载均衡机制分析
- 负载均衡VIP /LVS 的健康检测方式