nginx新增sticky cookie 分流模組

卡卡西村長發表於2016-10-09

需要下載nginx原始碼和sticky,在nginx配置檔案中新增sticky模組,然後重新編譯nginx。

 

#準備安裝基礎環境:
yum install gcc openssl-devel pcre-devel zlib-devel

#下載nginx1.6.3
wget http://nginx.org/download/nginx-1.6.3.tar.gz

#解壓縮nginx
tar xvf nginx-1.6.3.tar.gz

#安裝依賴的庫
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
yum -y install gd-devel
yum -y install perl-devel perl-ExtUtils-Embed
yum -y install GeoIP GeoIP-devel GeoIP-data
yum -y install gperftools-devel

#檢視nginx的配置
nginx -V

#加入sticky模組
cd nginx-1.6.3

./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 --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/root/nginx-sticky

#編譯
make

make install

#停止nginx
service nginx stop

#替換nginx
cp /usr/sbin/nginx /usr/sbin/nginx.bak.20161009
cp ./objs/nginx  /usr/sbin/nginx


#修改nginx配置:

upstream test_server {
        server 10.2.118.48:8002;
        server 10.2.118.49:8002;
        sticky;
}

 

#重啟nginx

service nginx restart

 

參考資料:

https://my.oschina.net/766/blog/156693

http://www.xuebuyuan.com/2184929.html

http://blog.chinaunix.net/uid-24625974-id-2894092.html

相關文章