本次实验平台:
物理机:windows8 64位 + Vmware workstation 11
虚拟机: 3台centos6.6 x86_64
虚拟机网络为host-only(vmnet1)
两台apache服务器:
1 hostname:node1.test.com ip:192.168.245.11
2 hostname:node2.test.com ip:192.168.245.12
一台nginx服务器:
hostname:node3.test.com ip:192.168.245.13
关闭iptables和selinux
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
安装nginx1.6.2:
将nginx-1.6.2.tar.gz(下载地址:http://nginx.org)上传至服务器node3.test.com
先安装openssl,openssl-devel,pcre
#yum -y install openssl openssl-devel pcre
创建用户和组:
#groupadd -g 200 nginx
#useradd -u 200 -g 200 -r nginx
然后安装nginx:
#tar -zxvf nginx-1.6.2.tar.gz
#cd nginx-1.6.2.tar.gz
#./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error_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-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
#make && make install
编写脚本:
#vi /etc/init.d/nginx
#!/bin/bash
#
# 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/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/nginx.lock
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 > /dev/null ] || 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 a+x /etc/init.d/nginx
添加进服务启动:
#chkconfig --add nginx
开机自启动:
#chkconfig nginx on
现在就可以用service nginx [start|stop|restart|reload] 来启动服务了。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
接着在另两台服务器安装apache:
#yum -y install httpd
接下来实现nginx反向代理,由nginx接受请求再将请求发至apache服务器:
启动两台apache:
#service httpd start
分别在两台apache上生成测试页面:
#echo "node1.test.com" > /var/www/html/index.html (192.168.245.11)
#echo "node2.test.com" > /var/www/html/index.html (192.168.245.12)
接着编辑nginx配置文件:
#vi /etc/nginx/nginx.conf
在server上下文中添加以下内容:
location /test/ {
proxy_pass http://192.168.245.11/pro/;
}
保存后重启nginx服务:
#service nginx reload
在node1上执行:
#mkdir /var/www/html/pro
#echo "this is 192.168.245.11/pro" > /var/www/html/pro/index.html
接着在windows8的浏览器中输入http://192.168.245.13/test/确定后会发现浏览器转向了192.168.245.11/pro下的主页,简单实现了反向代理。
接下来测试使用正则的情况:
#mkdir /var/www/html/abc
#echo "this is regex abc 192.168.245.12" >/var/www/html/abc/index.html
仍然在nginx.conf的server上下文中添加:
location ~* ^/abc {
proxy_pass http://192.168.245.12;
}
此时浏览器输入192.168.245.13/abc会发现浏览器转入192.168.245.12的abc中的index.html.
注意:192.168.245.12的/var/www/html下必须要有与location一致的abc目录,而且在192.168.245.12后不能添加任何URI,否则会出现错误!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
接着是nginx负载均衡:
编辑nginx.conf
在server上下文外(不能在location中)添加以下几行:
upstream nlbserver {
server 192.168.245.11 weight=1 max_fails=2 fail_timeout=1;
server 192.168.245.12 weight=1 max_fails=2 fail_timeout=1;
server 127.0.0.1:8080 backup;
}
#添加一个server上下文:
server {
location / {
proxy_pass http://nlbserver;
}
#在原主server上下文中添加:
listen 8080;
server_name localhost;
root /www/errorpage;
index index.html;
#在原主server上下文中注释以下几行:
# location / {
# root html;
# index index.html index.htm;
# }
保存退出后重启nginx服务器,在浏览器中输入192.168.245.13确定后刷新就会发现两台apache服务器页面交替出现。
其中weight是权重,max_fail表示最大错误次数,fail_timeout表示超时时间为一秒
下面的backup表示两台服务器均出现故障后出现的错误页面,这里错误页面所在位置nginx服务器上的/www/errorpage中,在里面定义一个页面:
echo "sorry.error occured" > /www/errorpage/index.html
当两台apache服务器stop后刷新就会看到错误页面了!
这就简单实现了nginx的负载均衡。