nginx使用yum/编译安装,心跳检查模块

1,yum直接安装

1)yum list installed | grep nginx
yum remove -y nginx.x86_64

image.png

2)使用新建的yum源。vim /etc/yum.repos.d/nginx.repo添加nginx仓库的yum配置。

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/  
gpgcheck=0
enabled=1

yum list |grep nginx
yum install nginx.x86_64

image.png

2,编译安装nginx。(如果需要单独加入模块,则必须使用编译安装)

1)编译安装操作

  • wget http://nginx.org/download/nginx-1.12.2.tar.gz
  • tar -zxvf nginx-1.12.2.tar.gz
  • cd /usr/local/src/nginx-1.12.2
  • mkdir -p /etc/nginx/
  • ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf- path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
  • 出现./configure: error: the HTTP rewrite module requires the PCRE library. 执行 yum -y install pcre-devel openssl openssl-devel
  • make & make install
  • nginx -v//查看版本
    nginx -V//查看版本以及已安装的模块。重新编译的时候,需要知道之前的模块

2)添加新的nginx模块。

  • wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
  • unzip master.zip
  • mv nginx_upstream_check_module-master nginx_http_upstream_check_module
  • cd /usr/local/src/nginx-1.12.2
  • patch -p1 < /usr/local/src/nginx_http_upstream_check_module/check_1.12.1+.patch//安装对应版本的补丁check_version+.path
    ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log- -path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
  • make && make install
  • cp objs/nginx /usr/sbin/nginx //覆盖原来的nginx二进制文件
  • nginx -V//查看新的模块是否安装成功。

3)使用nginx_http_upstream_check_module。
使用http1.1长连接。
proxy_http_version 1.1; 全局增加

#\r\n之后不能有空格;Host:xxxx在http1.1下必须增加host头,或者使用http1.0
check_keepalive_requests 100;
check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\nHost: api.xxx.com\r\n\r\n"; # \r\n后面不能有空格。
check_http_expect_alive http_2xx http_3xx;
check interval=3000 rise=2 fall=5 timeout=1000 type=http

看tomcat的访问日志(心跳检查的日志可以在这里看到)
tail -400f /usr/local/tomcat/duobeiyun-api-server/logs/localhost_access_log2017-12-20.txt

image.png

使用tcp的时候,只要进程在,端口可用就认为在线。(mysql、redis连接不上,也默认up) 项目启动,停止,重启过程中,也认为up

默认使用check_http_send "GET / HTTP/1.0\r\n\r\n";
check interval=3000 rise=1 fall=3 timeout=4000;
间隔3s检查一次;1次请求成功则标记up;3次请求失败标记down;请求超时时间为4s。

upstream backend_server {#upstream模块
        server 10.103.131.42:8080;#后端upstream server
        server 10.103.131.42:8090;
        server 10.103.131.42:8100;
        keepalive 32;#指定每个worker和后端(所有)server保持的最大长连接数。
        check interval=3000 rise=1 fall=3 timeout=4000;#check_module的指令
        check_keepalive_requests 100;#默认是1,一个连接处理一个request就关闭。
        check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";#使用HEAD请求减网络传输;keep-alive模式使用长连接。默认方式是GET
        check_http_expect_alive http_2xx http_3xx;#当状态码为指定值是,认为server响应OK。
        check_shm_size 5M;//默认值为1M,当检测几百台时,内存可能不够。
     }
location /upstream/status {#upstream健康检查配置
        check_status;#匹配url,使用check_status指令。
        access_log off;#不开启访问日志
        allow 127.0.0.1;
        allow 10.103.131.0/24;#允许的ip段
        deny all;#进制其他ip
    }
//--with-http_stub_status_module使用stub status的模块
location /ngx_status {#nginx
         stub_status on;
         access_log off;
         allow 127.0.0.1;
         allow 124.204.55.50;
         deny all;
      }

Active connections: 3 //活跃的连接数
server accepts handled requests
 18 18 54//处理的18个连接,成功建立18次握手,处理了54次请求。
Reading: 0 Writing: 1 Waiting: 2 // active - (reading+writing) = waiting(空闲的驻留连接)

curl 127.0.0.1/upstream/status??format=html//默认显示的格式。
format=csv
format=json

3,nginx基本命令。

1)查看/usr/sbin/nginx -V查看nginx版本和已安装的模块(-v只查看nginx的版本)
2)which nginx/usr/sbin/nginx 查看nginx安装位置
3)nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok默认检查/etc/nginx/nginx.conf
nginx -t -c /etc/nginx/nginx.conf-c指定nginx的配置文件
4)nginx & //以后台进程的方式启动nginx nginx -c /etc/nginx/nginx.conf &
5)nginx -s stop;nginx -s reload

4,nginx启动脚本。

vim /etc/init.d/nginx chmod +x /etc/init.d/nginx

#!/bin/sh

# Source function library.
. /etc/rc.d/init.d/functions

nginx=${NGINX-/usr/sbin/nginx} #如果NGINX没有设定,则使用/usr/sbin/nginx,否则返回${NGINX}
prog=`/bin/basename $nginx` # /bin/basename /usr/sbin/nginx --> nginx
conffile=${CONFFILE-/etc/nginx/nginx.conf} # 配置文件的位置
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/var/run/nginx.pid} # pid文件
RETVAL=0

start() {
    echo -n $"Starting $prog: "

    daemon --pidfile=${pidfile} ${nginx} -c ${conffile} # 以后台方式运行,并指定配置文件
    RETVAL=$? # 上个命令的退出状态或者函数的返回值,执行成功会返回 0,失败返回 1。
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}  #[]用来比较
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} ${prog} #kill掉进程
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} #删除pid和lock文件
}

reload() {
    echo -n $"Reloading $prog: "
    killproc -p ${pidfile} ${prog} -HUP
    RETVAL=$?
    echo
}

configtest() {
    if [ "$#" -ne 0 ] ; then # 获取参数的个数
        case "$1" in
            -q)
                FLAG=$1
                ;;
            *)
                ;;
        esac
        shift
    fi
    ${nginx} -t -c ${conffile} $FLAG #执行nginx语法测试
    RETVAL=$?
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
        rh_status >/dev/null 2>&1 && exit 0
        start
        ;;
    stop)
        stop
        ;;
    status)
        rh_status
        RETVAL=$?
        ;;
    restart)
        configtest -q || exit $RETVAL
        stop
        start
        ;;
    configtest)
        configtest
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|reload|status|help|configtest}"
        RETVAL=2
esac

exit $RETVAL
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容