1.安装所需依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
###2.安装pcre, nginx_upstream_check_module
PCRE 作用是让 Ngnix 支持 Rewrite 功能。 ####**2.1 下载pcre **
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
####**2.2 解压pcre **
tar zxvf pcre-8.35.tar.gz
####2.2 编译和安装
cd pcre-8.35
./configure
make && make install
####**3.1 下载nginx_upstream_check_module ** nginx_upstream_check_module 的项目在github 上,我是安装了git之后clone下来
yum install -y git
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
####**4.1 下载nginx ** 试了下最新稳定版1.10.1,却配置不成功,不知道为什么,试了1.9.9版本也不行。最后还是换回了自己熟悉的1.8.1版本。有知道问题的请留言给我吧,谢谢。
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
PCRE ,nginx_upstream_check_module ,nginx 我是放在同一目录下。
在nginx目录先patch 一下
patch -p1 < ../nginx_upstream_check_module/check_1.7.5+.patch
patch选择哪些版本,github上有说明 ####**4.2 配置和安装nginx **
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=../pcre-8.35 --add-module=../nginx_upstream_check_module/
make
make install
设置nginx命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
最后在/usr/local/nginx/conf/nginx.conf配置负载均衡和监测功能
worker_processes 1;
error_log logs/error.log warn;
pid logs/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
upstream juice-api{
server 192.168.1.121:8080;
server 192.168.1.132:8080;
server 192.168.1.120:8080;
check interval=5000 rise=1 fall=3 timeout=4000;
#check interval=3000 rise=2 fall=5 timeout=1000 type=http;
#check_http_send "HEAD / HTTP/1.0\r\n\r\n";
#check_http_expect_alive http_2xx http_3xx;
}
#安全防御模块,触发条件,所有访问ip 限制每秒80个请求
limit_req_zone $binary_remote_addr zone=one:10m rate=80r/s;
server {
listen 8000;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
proxy_pass http://juice-api;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
limit_req zone=one burst=5 nodelay;
}
location /status {
check_status;
access_log off;
allow 192.168.1.93;
deny all;
}
}
}
配置太多就不粘贴了
最后启动,查看监测
参考资料: https://github.com/yaoweibin/nginx_upstream_check_module
http://tengine.taobao.org/document_cn/http_upstream_check_cn.html