Nginx+apache 构筑 Web 服务器集群的负载均衡

Nginx+apache 构筑 Web 服务器集群的负载均衡
一、nginx 配置反向代理
1、安装 nginx
环境:
OS:centos7.2
nginx:192.168.31.83
apache1:192.168.31.141
apache2:192.168.31.250
安装 zlib-devel、pcre-devel 等依赖包

Nginx+apache 构筑 Web 服务器集群的负载均衡_第1张图片

注:
结合 proxy 和 upstream 模块实现后端 web 负载均衡
使用 proxy 模块实现静态文件缓存结合 nginx 默认自带的 ngx_http_proxy_module 模块 和 ngx_http_upstream_module 模块
实现后端服务器的健康检查,也可以使用第三方模块nginx_upstream_check_module使用 nginx-sticky-module 扩展模块实现 Cookie 会话黏贴(保持会话)使用 ngx_cache_purge 实现更强大的缓存清除功能上面提到的 2 个模块都属于第三方扩展模块,需要提前下好源码,然后编译时通过--add-moudle=src_path 一起安装。
2、安装Nginx

(1)添加www组
(2)创建Nginx运行账户www并加入到www组,不允许www用户直接登录系统。

Nginx+apache 构筑 Web 服务器集群的负载均衡_第2张图片

Nginx+apache 构筑 Web 服务器集群的负载均衡_第3张图片

[root@localhost nginx-1.14.0]#make && make install
如果安装报错就删除掉:
1、/usr/local/nginx1.10/
2、/usr/local/sbin/nginx
注:nginx 的所有模块必须在编译的时候添加,不能再运行的时候动态加载。
优化 nginx 程序的执行路径
[root@www nginx-1.10.2]# ln -s /usr/local/nginx1.10/sbin/nginx /usr/local/sbin/
[root@www nginx-1.10.2]# nginx -t
[root@www nginx-1.10.2]# mkdir -p /var/tmp/nginx/client
[root@www nginx-1.10.2]# chown -R www:www /var/tmp/nginx/
[root@www nginx-1.10.2]# nginx -t
nginx: the configuration file /usr/local/nginx1.10/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1.10/conf/nginx.conf test is successful
2、编写 nginx 服务脚本:
[root@www ~]# vi /etc/init.d/nginx 内容如下:
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx1.10/sbin/nginx"
PIDF="/usr/local/nginx1.10/logs/nginx.pid"
case "$1" in
start)
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
echo "Nginx service already running."
else
$PROG -t &> /dev/null
if [ $? -eq 0 ] ; then
$PROG
echo "Nginx service start success."
else
$PROG -t
fi
fi
;;
stop)
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
kill -s QUIT $(cat $PIDF)
echo "Nginx service stop success."
else
echo "Nginx service already stop"
fi
;;
restart)
$0 stop
$0 start
;;
status)
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
echo "Nginx service is running."
else
echo "Nginx is stop."
fi
;;
reload)
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
$PROG -t &> /dev/null
if [ $? -eq 0 ] ; then
kill -s HUP $(cat $PIDF)
echo "reload Nginx config success."
else
$PROG -t
fi
else
echo "Nginx service is not run."
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
[root@www ~]# chmod +x /etc/init.d/nginx
[root@www ~]# chkconfig --add nginx
[root@www ~]# chkconfig nginx on
Nginx+apache 构筑 Web 服务器集群的负载均衡_第4张图片
两台服务器安装Apache

Nginx+apache 构筑 Web 服务器集群的负载均衡_第5张图片

Nginx+apache 构筑 Web 服务器集群的负载均衡_第6张图片

访问Nginx成功。
二、验证

Nginx+apache 构筑 Web 服务器集群的负载均衡_第7张图片

清除缓存:
上述配置的 proxy_cache_purge 指令用于方便的清除缓存,但必须按照第三方的
ngx_cache_purge 模块才能使用
使用 ngx_cache_purge 模块清除缓存(直接删除缓存目录下的文件也算一种办法):
GET 方式请求 URL
即使用配置文件中的 location ~ /purge(/.*)
浏览器访问 http://192.168.157.131/purge/your/may/path 来清除缓存

Nginx+apache 构筑 Web 服务器集群的负载均衡_第8张图片

缓存清除成功。
备注:
(1)purge 是 ngx_cache_pure 模块指令
(2)your/may/path 是要清除的缓存文件 URL 路径
2)若只有一台客户端要验证负载均衡和健康检查可以先关掉缓存功能和保持 session 会话
#proxy_buffering off;       
#sticky
关闭其中一台Apache服务器










你可能感兴趣的:(Linux)