Ubuntu源码安装nginx(整合session_sticky模块)

背景

nginx做为七层负载均衡软件,负载的方式有:1.ip hash 2.轮询 3.带权重轮询 4.sticky session(粘滞session)大概这一些。 其中sticky session能够保证同一客户端请求能转发到后端固定的一台服务器上。 tengine目前已经集成了该特性,但我试验没成功。有时间再试一试。html

###sticky session原理nginx

客户端第一次请求nginx,nginx会根据轮询算法,选择一台后端服务器,而后nginx在响应头上增长Set-Cookie:route=md5, 客户端下一次请求nginx时,会带上这个route=md5,nginx会根据这个route来选择上一次请求时的后端服务器。这就保证了nginx会将同一个客户端的请求都会转交给同一后端服务器。web

(client)                             (nginx)                      (upstream servers)
    >-- GET /URI1 HTTP/1.0 -----------> |
                                        | *** nginx choose one upstream by RR ***
                                        | >----- GET /URI1 HTTP/1.0   ----> |
                                        | <------- HTTP/1.0 200 OK -------< |
    <-- HTTP/1.0 200 OK --------------< |
        Set-Cookie: route=md5(upstream) |
                                        |
    >-- GET /URI2 HTTP/1.0 -----------> |
        Cookies: route                  |
                                        | *** nginx redirect to "route" ***
                                        | >----- internal fetch /URI2 ----> |
                                        | <--- internal response /URI2 ---< |
    <-- HTTP/1.0 200 OK --------------< |
                                      (...)

准备工做,下载所需软件

因为国内网络环境的缘由,能够改用网易163的软件镜像源:算法

如何修改网易163的软件镜像源,能够参照个人这篇文章:http://my.oschina.net/u/1167421/blog/604492ubuntu

ubuntu14.04默认没有gcc,g++segmentfault

apt-get install -y gcc g++

通常安装ngxin时,都会安装如下这些模块: gzip模块须要 zlib 库后端

获取zlib编译安装包,在http://www.zlib.net/上能够获取当前最新的版本。服务器

rewrite模块须要 pcre 库网络

获取pcre编译安装包,在http://www.pcre.org/上能够获取当前最新的版本session

ssl 功能须要openssl库

获取openssl编译安装包,在http://www.openssl.org/source/上能够获取当前最新的版本。

下载sticky sesssion

https://code.google.com/p/nginx-sticky-module/

下载nginx(我用的是1.6.3) http://nginx.org/en/download.html

注:下载下来能够放到/usr/local/src目录下

安装依赖库

安装pcre

解压缩pcre-xx.tar.gz包。

     进入解压缩目录,执行./configure。

     make & make install

安装openssl

解压缩openssl-xx.tar.gz包。

     进入解压缩目录,执行./config。

     make & make install

安装zlib

解压缩zlib-xx.tar.gz包。

     进入解压缩目录,执行./configure。

     make & make install

下载nginx

源码安装nginx

修改nginx-sticky-module-1.1源码:

解压nginx-sticky-module-1.1.tar.gz cd nginx-sticky-module-1.1

新版nginx和nginx-sticky-module-1.1编译会有点问题,应该是nginx新版本没有相应的nginx-sticky-module。 解决办法是把nginx-sticky-module-1.1/ngx_http_sticky_misc.c的281行修改成: digest->len = ngx_sock_ntop(in,sizeof(struct sockaddr_in), digest->data, len, 1); 注:若是使用nginx-1.4.7那么就不须要改nginx-sticky-module-1.1源码,使用nginx-1.6.3作粘性session,还有一个模块能够使用https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng不过我没试过。

解压nginx-1.6.3.tar.gz

目录结构

进入nginx-1.6.3

./configure --prefix=/usr/local/webserver/nginx --with-openssl --with-http_ssl_module  --with-http_realip_module --with-http_gzip_static_module --with-http_sub_module  --add-module=../nginx-sticky-module-1.1

因为没有指定openssl的源码路径,因此报了如下错误:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

能够经过指定open-ssl源码目录来解决--with-openssl=../openssl-xxx

./configure --prefix=/usr/local/webserver/nginx --with-openssl=../openssl-1.0.2e --with-http_ssl_module  --with-http_realip_module --with-http_gzip_static_module --with-http_sub_module  --add-module=../nginx-sticky-module-1.1

接下来就能够:

make -j 4

make install

完成安装

###使用简单说明

最简单的用法:

upstream {
  sticky;
  server 127.0.0.1:9000;
  server 127.0.0.1:9001;
  server 127.0.0.1:9002;
}

sticky命令格式:

sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback];

sticky命令参数:

输入图片说明

参考资料

https://code.google.com/p/nginx-sticky-module/ http://www.cnblogs.com/skynet/p/4146083.html http://www.javashuo.com/article/p-yheskdoy-ha.html

相关文章
相关标签/搜索