首页 最新文章网站Nginx正文

nginx负载均衡实现实战(续)

    7月11日的时候写了一篇博文主要介绍了自建负载均衡的背景和原因,以及实现方式,但也只是在一定程度上实现了负载均衡,由于ip_hash算法的局限性,针对有CDN加速网站,这种方法极易导致负载的不均衡,事实情况也是这样的,所以寻找一种更科学的方法来代替ip_hash算法以成为当务之急。
     方法总比问题多,只要多看多想多研究,其实新方案的出炉完全是意外产生的。
     负载均衡的实施必然会面临会话保持的问题,如何使同一来源的访问总是由同一台后端服务器提供服务,通过负载均衡服务器直接植入cookie并设定过期时间是一种方法,或者重写cookie,这两种思路实际上都是从服务器端set-cookie的,也就是说是在响应的时候才设置cookie,再次请求的时候根据这个cookie来选择同一台服务器,但是如果使用cdn加速,大量的静态页面会被缓存到各个CDN节点,当然cookie信息也会被同时缓存,这就会产生问题,假设第一次访问的时候缓存的是A服务器的cookie信息,下次会话的过程中负载均衡服务器会再次写入一个cookie,而这个cookie很有可能和缓存的不一致,这就会导致同一个会话两次访问的可能不是同一台服务器从而导致会话丢失,想解决这个问题有3个方案:
1、想办法让负载均衡服务器对CDN缓存的静态内容不设置cookie,仅对动态内容设置
2、对静态内容设置专门的独立域名,对该域名进行CDN加速,对主域名做负载均衡,实现cookie分离
3、利用客户端的cookie信息作为会话保持的依据

这里仅对第3种方案进行可行性分析和具体实现。
接上文的nginx负载均衡,其有一个模块可以实现这种需求,那就是sticky,默认nginx是不装载该模块的,所以第一步就是装载,原来nginx是使用脚本一键安装的,所以需要找到nginx的安装包的具体路径,并把sticky模块下载到该位置
/home/tmp/sh-1.2.1/sh-1.2.1/nginx-1.0.15

nginx-sticky-module的下载命令:wget https://nginx-sticky-module.googlecode.com/files/nginx-sticky-module-1.1.tar.gz
解压tar -xzvf nginx-sticky-module-1.1.tar.gz

nginx的每次新增模块都需要重写编译和安装的,为了确保对之前的既有功能不影响,需要查看一下当时安装nginx时的具体配置.
命令:nginx -V
--user=www --group=www --prefix=/alidata/server/nginx --with-http_stub_status_module --without-http-cache --with-http_SSL_module --with-http_gzip_static_modul

加载新模块,同时调整user和group(这一步不是必然要做,是顺便做了达到用户自动分配的目的)
[root@AY1309031759587435f2Z nginx-1.0.15]# ./configure --user=APPuser --group=dba --prefix=/alidata/server/nginx --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_GZip_static_module --add-module=/home/tmp/sh-1.2.1/sh-1.2.1/nginx-1.0.15/nginx-sticky-module-1.1
checking for OS
 + linux 2.6.18-164.el5 x86_64
checking for C compiler ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... Not found
checking for sys/paRAM.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for sched_setaffinity() ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for SO_SETFIB ... not found
checking for SO_ACCEPTFILTER ... not found
checking for TCP_DEFER_ACCEPT ... found
checking for accept4() ... not found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system endianess ... little endianess
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
configuring additional modules
adding module in /home/tmp/sh-1.2.1/sh-1.2.1/nginx-1.0.15/nginx-sticky-module-1.1
 + ngx_http_sticky_module was configured
checking for PCRE library ... found
checking for openssl library ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/alidata/server/nginx"
  nginx binary file: "/alidata/server/nginx/sbin/nginx"
  nginx configuration prefix: "/alidata/server/nginx/conf"
  nginx configuration file: "/alidata/server/nginx/conf/nginx.conf"
  nginx pid file: "/alidata/server/nginx/logs/nginx.pid"
  nginx error log file: "/alidata/server/nginx/logs/error.log"
  nginx http access log file: "/alidata/server/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
[root@AY1309031759587435f2Z nginx-1.0.15]#
在进行编译和安装make&make install,这样就实现了sticky模块的加载。

nginx负载均衡配置
    upstream www.test.com {
        sticky name=SLBID expires=1h path=/;
        server 10.10.1.101 weight=4;
        server 10.10.1.102  weight=6;
    }

其中name参数可以自定义cookie名称,expires设置cookie的过期时间,path设置是负载均衡的对象路径。
通过监控两台服务器的后台日志,可以证明实现了负载均衡
通过监控相关页面的cookie信息,SLBID这个cookie已经被植入,通过不停的刷新页面也不会变化,证明会话保持住了
后续就是观察实际情况是否还存在会员自动退出的情形发生。
至此实现了自建负载均衡服务器的目的,摆脱了使用第三方产品不灵活的限制,同时也节省了一笔流量费用。

评论

觉得有用就打赏吧
关注本站公众号,享受更多服务!
联系方式
QQ:########
地址:中国·辽宁
Email:2727987445#qq.com
Copyright ©2015-2023.Powered by 云水客 | 网站地图 | 辽ICP备14000512号-5