在CentOS上重新編譯nginx,加sticky模塊

爲了達到高可用目的,一個應用一般會部署多個實例,前面用nginx做負載。有些時候我們希望一個用戶的所有請求都打到其中一個實例上(比如會話或者狀態不能在服務集羣所有實例間進行共享時),nginx官方默認的解決方案是通過ip_hash實現,但是ip hash 不夠準確,ip hash其實就是把客戶端ip v4地址4段中的前3段拿來做hash運算得到一個hash值,通過該hash值決定請求打到哪個服務上;在很多內外應用中,內網用戶很多都在一個網段下,導致ip地址的前3段基本都一樣的,這導致所有請求都會打到其中一個服務實例上,而其他服務實例沒有負載。

sticky負載模式是通過分配的路由碼實現客戶端和後端服務實例綁定。具體處理過程爲:客戶端第一次向服務發起請求時不帶路由碼,nginx通過輪休的負載方式把請求打到其中的一個後端服務實例,確定具體服務實例後,nginx分配一個路由碼帶到響應cookies裏,客戶端在cookies中保存該路由碼,每次請求的時候都帶上,nginx接收到請求後,解析請求頭裏面的route cookies值,根據該值把請求轉發到該值綁定的後端服務實例,達到回話粘粘的效果。

sticky負載模式必須要客戶端支持cookies。

重新編譯nginx

安裝基礎依賴包,下載sticky模塊和nginx源碼和其他模塊。

# yum -y install wget tar git gcc make pcre pcre-devel zlib zlib-devel openssl openssl-devel GeoIP GeoIP-devel#
# cd /tmp
# git clone https://github.com/gnosek/nginx-upstream-fair.git
# git clone https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng.git
# git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
# wget http://nginx.org/download/nginx-1.16.1.tar.gz

解壓編譯

# tar -xvzf nginx-1.16.1.tar.gz
# cd nginx-1.16.1

# ./configure --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module --with-http_geoip_module --add-module=/tmp/nginx-sticky-module-ng --add-module=/tmp/nginx_upstream_check_module
# make
# make install
# openssl dhparam -out /etc/nginx/dhparams.pem 2048

執行下面的命令,創建需要的目錄和權限

# mkdir /etc/nginx/conf.d 
# mkdir -p /var/lib/nginx/tmp/client_body 
# chown -R nginx.nginx /var/lib/nginx/ 
# chmod -R 770 /var/lib/nginx/

創建初始化腳本

添加配置文件/etc/init.d/nginx讓nginx隨系統啓動一起啓動。

當我們執行service nginx命令的時候,這個文件將會被執行。

# chmod +x /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# pidfile:     /var/run/nginx.pid
# user:        nginx

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

lockfile=/var/run/nginx.lock

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

讓服務隨系統啓動

# chkconfig --add nginx
# chkconfig --level 345 nginx on

重啓服務

# service nginx restart

檢查編譯模塊

# nginx -V

輸出

nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module --with-http_geoip_module --add-module=/tmp/nginx-sticky-module-ng --add-module=/tmp/nginx_upstream_check_module

使用

/etc/nginx/conf.d下新建配置文件sticky_test.conf,upstream負載模式設置爲sticky,配置內容如下。

upstream sticky.itclj.lo {
    sticky;
    server 127.0.0.1:8180;
    server 127.0.0.1:8280;
}

server {
        listen 80;
        server_name  sticky.itclj.com;
        location  ~*^.+$ {
           proxy_pass  http://sticky.itclj.lo;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection "upgrade";
        }
}

在這裏插入圖片描述

  • nginx下負載多個Tomcat請參考:https://blog.csdn.net/clj198606061111/article/details/22621003
  • sticky官方完整配置說明:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#sticky
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章