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
  • 出現(xiàn)./configure: error: the HTTP rewrite module requires the PCRE library. 執(zhí)行 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//安裝對應(yīng)版本的補(bǔ)丁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二進(jìn)制文件
  • 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的時候,只要進(jìn)程在,端口可用就認(rèn)為在線。(mysql、redis連接不上,也默認(rèn)up) 項目啟動,停止,重啟過程中,也認(rèn)為up

默認(rèn)使用check_http_send "GET / HTTP/1.0\r\n\r\n";
check interval=3000 rise=1 fall=3 timeout=4000;
間隔3s檢查一次;1次請求成功則標(biāo)記up;3次請求失敗標(biāo)記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保持的最大長連接數(shù)。
        check interval=3000 rise=1 fall=3 timeout=4000;#check_module的指令
        check_keepalive_requests 100;#默認(rèn)是1,一個連接處理一個request就關(guān)閉。
        check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";#使用HEAD請求減網(wǎng)絡(luò)傳輸;keep-alive模式使用長連接。默認(rèn)方式是GET
        check_http_expect_alive http_2xx http_3xx;#當(dāng)狀態(tài)碼為指定值是,認(rèn)為server響應(yīng)OK。
        check_shm_size 5M;//默認(rèn)值為1M,當(dāng)檢測幾百臺時,內(nèi)存可能不夠。
     }
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;#進(jìn)制其他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 //活躍的連接數(shù)
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//默認(rèn)顯示的格式。
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默認(rèn)檢查/etc/nginx/nginx.conf
nginx -t -c /etc/nginx/nginx.conf-c指定nginx的配置文件
4)nginx & //以后臺進(jìn)程的方式啟動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沒有設(shè)定,則使用/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=$? # 上個命令的退出狀態(tài)或者函數(shù)的返回值,執(zhí)行成功會返回 0,失敗返回 1。
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}  #[]用來比較
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} ${prog} #kill掉進(jìn)程
    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 # 獲取參數(shù)的個數(shù)
        case "$1" in
            -q)
                FLAG=$1
                ;;
            *)
                ;;
        esac
        shift
    fi
    ${nginx} -t -c ${conffile} $FLAG #執(zhí)行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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容