大家都知道,每年双十一是各个电商平台疯狂“收割”的日子,同事各位顾客们也能在这一天网购获得很大的实惠。那么,同时有那么多人一起去“挤”网站,网站又是如何应对这么大的并发请求呢?通过负载均衡,可以使网站运营者在软件层面上应付“大流量”。当然,还需要硬件支持,在这里不做赘述。今天我们就来探讨一下,如何搭建Nginx实现高可用Web负载均衡?
一、什么是负载均衡?为什么要使用负载均衡?
首先我们得明白什么是负载均衡?首先来看一下百度百科上对于负载均衡的介绍。
负载均衡:建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
这样笼统来看,我想有很多人都看不明白,那么下面我将用大白话给大家介绍一下到底什么是负载均衡。
现在网站的架构已经从C/S模式转变为B/S模式,对于这两种模式的区别,大家没必要纠结,有兴趣的同学可以给我留言,我会在后面为大家讲解,现在只需要知道C/S模式是有一个专门的客户端,而B/S模式是将浏览器作为客户端。当用户在浏览器上输入一个网址按下回车键后,就会产生一个请求,在远方的服务器会处理这个请求,根据这个请求来生成用户想要的页面,然后将这个页面响应给浏览器,这样用户就能看到他想要看到的东西。我们知道,一台服务器处理数据(请求也是一种数据)的能力是有限的,当有大量的用户同时在浏览器上输入网址并按下回车键后,就会有大量的请求产生,远方的服务器就不得不处理这些请求,由于请求数量过多,服务器处理的效率就会变慢,响应时间就会变长,这样用户就不能在可以忍受的时间内看到自己想看到的东西,严重影响体验效果。更严重一点,如果请求数量超过了这台服务器所能处理的最大请求,服务器就会崩溃,直接导致网站瘫痪。
那么,有什么方法能够解决这个问题呢?答案就是建立一个集群(就是一群服务器),通过集群的力量来提高服务端的数据处理能力,因为一台服务器的处理能力肯定比不上多台服务器的处理能力。
现在我们在来描述一下用户请求页面的过程:首先用户在浏览器输入网址并按下回车键,然后会产生一个请求,远方的服务器会处理这个请求…等等,现在远方有很多服务器,到底哪个服务器来处理这个请求呢,总不能所有的服务器都处理这个请求吧。哪个服务器处理这个请求?大家明白了吧,这就是负载均衡所要解决的问题。回到上边请求页面的过程,这个请求此时会被一台专门的服务器来处理,这台服务器其实就是个集群的老大,他负责把这个请求派给下面哪个小弟(服务器)来处理,处理完之后返回页面用户。当有多个请求同时发生时,集群的老大可以将请求派给不同的小弟,这样处理的效率就会大幅提升,充分发挥集群的力量,至于哪个请求到底派给哪个小弟,这就是调度策略的问题了。
好了,到现在大家应该都明白什么是负载均衡了,为了让大家印象更加深刻,大家可以看一下下面的图,便于理解记忆。
二、Nginx安装与配置
1.安装Nginx
- 安装C++编译环境
yum -y install gcc
- 安装pcre
yum -y install pcre-devel
- 安装zlib
yum -y install zlib-devel
- 安装Nginx
(1)下载Nginx包
进入链接:http://nginx.org/download/nginx-1.8.1.tar.gz,下载Nginx1.8安装包,用命令tar -zxvf nginx-1.8.1.tar.gz
解压。
**(2)安装**
定位到nginx 解压文件位置,执行编译安装命令。
```
[root@localhost nginx-1.8.1]# pwd
/usr/local/nginx/nginx-1.8.1
[root@localhost nginx-1.8.1]# ./configure && make && make install
```
(3)启动与停止Nginx
使用命令./nginx &
启动Nginx[root@localhost /]# cd /usr/local/nginx/ [root@localhost nginx]# ll 总用量 0 drwx------ 2 nobody root 6 8月 10 15:03 client_body_temp drwxr-xr-x 2 root root 333 8月 10 14:47 conf drwx------ 2 nobody root 6 8月 10 15:03 fastcgi_temp drwxr-xr-x 2 root root 40 8月 10 14:47 html drwxr-xr-x 2 root root 41 8月 10 15:03 logs drwxr-xr-x 9 root root 186 8月 10 14:46 nginx-1.8.1 drwx------ 2 nobody root 6 8月 10 15:03 proxy_temp drwxr-xr-x 2 root root 19 8月 10 14:47 sbin drwx------ 2 nobody root 6 8月 10 15:03 scgi_temp drwx------ 2 nobody root 6 8月 10 15:03 uwsgi_temp [root@localhost nginx]# cd sbin/ [root@localhost sbin]# ./nginx & #表示启动nginx
如果启动的时候发现默认端口80被占用,可使用命令netstat -lnp|grep 端口号
找到80端口的进程,并使用kill -9 进程ID
“杀”掉进程,再启动nginx即可。
[root@localhost sbin]# netstat -lnp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1064/nginx: worker
unix 2 [ ACC ] STREAM LISTENING 28046 2178/dbus-daemon @/tmp/dbus-mQQQjFO2Lk
unix 2 [ ACC ] STREAM LISTENING 18802 687/lsmd /var/run/lsm/ipc/simc
unix 2 [ ACC ] STREAM LISTENING 18804 687/lsmd /var/run/lsm/ipc/sim
unix 2 [ ACC ] STREAM LISTENING 27804 2145/gnome-keyring- /run/user/1000/keyring/control
unix 2 [ ACC ] STREAM LISTENING 24980 1943/X /tmp/.X11-unix/X0
[root@localhost sbin]# kill -9 1064
[root@localhost sbin]# netstat -lnp|grep 80
unix 2 [ ACC ] STREAM LISTENING 28046 2178/dbus-daemon @/tmp/dbus-mQQQjFO2Lk
unix 2 [ ACC ] STREAM LISTENING 18802 687/lsmd /var/run/lsm/ipc/simc
unix 2 [ ACC ] STREAM LISTENING 18804 687/lsmd /var/run/lsm/ipc/sim
unix 2 [ ACC ] STREAM LISTENING 27804 2145/gnome-keyring- /run/user/1000/keyring/control
unix 2 [ ACC ] STREAM LISTENING 24980 1943/X /tmp/.X11-unix/X0
[root@localhost sbin]# ./nginx &
[1] 9921
(4)重启Nginx
使用命令./nginx -s reload
重启Nginx,如果出现找不到nginx.pid,参考如下。[root@localhost sbin]# ./nginx -s reload nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) [root@localhost sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf [root@localhost sbin]# cd ../logs/ [root@localhost logs]# ls access.log error.log nginx.pid [root@localhost logs]#
(5)Nginx快捷启动和开机启动配置
使用命令vim /etc/rc.d/init.d/nginx
编辑Nginx快捷启动脚本【注意Nginx安装路径,需要根据自己的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: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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="/usr/local/nginx/conf/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' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
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
为启动脚本授权 并加入开机启动
[root@localhost init.d]# chmod -R 777 /etc/rc.d/init.d/nginx
[root@localhost init.d]# chkconfig nginx
启动Nginx并将Nginx加入系统环境变量,最后使用service nginx start启动
[root@localhost init.d]# chmod -R 777 /etc/rc.d/init.d/nginx
[root@localhost init.d]# chkconfig nginx
[root@localhost init.d]# ./nginx start
[root@localhost init.d]# echo ‘export PATH=$PATH:/usr/local/nginx/sbin’>>/etc/profile && source /etc/profile
[root@localhost init.d]# service nginx start
Starting nginx (via systemctl): Warning: nginx.service changed on disk. Run ‘systemctl daemon-reload’ to reload units.
[ 确定 ]
使用命令分别启动、停止、重启:service nginx (start|stop|restart)
2.Nginx的安装
https://www.jianshu.com/p/2a31c0b8bec9