前言;
上篇博文《keepalived_nginx实现discuz负载均衡和高可用》讲到,由于nginx将health_check功能放入到了商业版本,导致社区版本的nginx进行负载均衡,无法对后端的RS主机进行健康状态检测,所以现在准备使用tengine来取代nginx。我们只需要将前一章节VS主机上的nginx替换为tengine即可。
配置:
Host VS1
卸载nginx,安装tengine
# yum remove -y nginx
# yum groupinstall -y "Development tools" "Server Platform Development"
# yum install -y pcre-devel
# wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz
# tar -xf tengine-2.0.3.tar.gz
# cd tengine-2.0.3
# ./configure
# make && make install
# echo "PATH=/usr/local/nginx/sbin/:$PATH" > /etc/profile.d/nginx.sh
# exec bash
创建sorry_server页面文件
# cp /usr/local/nginx/html/index.html{,.bak}
# echo "服务维护中,请稍后访问." > /usr/local/nginx/html/index.html
修改tengine配置文件
# vim /usr/local/nginx/conf/nginx.conf
http{
...
# 上游服务器, 即后端RS服务器.
upstream backend {
server 10.0.0.101 weight=1;
server 10.0.0.102 weight=1;
# sorry_server
server 10.0.0.111:8080 backup;
server 10.0.0.112:8080 backup;
# 健康检查
check interval=3000 rise=1 fall=3 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name localhost;
# 当nginx将php代码送至后端RS处理时请求头中的Host值会是backend.
# php代码在RS上处理时,其内部代码会去请求图片/层叠样式表等静态资源以填充页面.
# 而php代码去请求静态资源时使用的是如http://backend/xxxx.gif这样的url,自然是取不到的.
# 所以我们要在nginx向后代理遇到Host为backend时,将其转换为127.0.0.1.
set $my_host $http_host;
if ($http_host = "backend") {
set $my_host "127.0.0.1";
}
location / {
proxy_pass http://backend;
proxy_set_header Host $my_host;
}
}
server {
listen 8080;
server_name localhost;
charset utf-8;
root /usr/local/nginx/html;
index index.html index.htm;
# sorry_server仅提供主页面, 访问其它资源也转至主页面.
location ~ .* {
error_page 404 /;
}
}
启动服务:
# nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
# nginx
Host VS2 做相同的配置
测试:
轮流关闭HostRS1和HostRS2,模拟RS主机宕机情形,可以发现用户仍然可以正常访问,说明用户请求被调度到了正常的RS主机上
# tail /usr/local/nginx/logs/error.log
2016/05/14 14:57:37 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:41 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:46 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:50 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:55 [error] 26112#0: check time out with peer: 10.0.0.101:80
2016/05/14 14:57:59 [error] 26112#0: check time out with peer: 10.0.0.101:80
同时关闭RS1 和 RS2 会提示用户服务维护中
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com