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

相關文章
相關標籤/搜索