Nginx可以幹許多事情,在這裡我們主要使用Nginx的反向代理與負載均衡功能。

1、Nginx的下載安裝

在安裝Nginx前需要安裝如下軟體:

GCC  Nginx是C寫的,需要用GCC編譯

PCRE(Perl Compatible Regular Expression)  Nginx的Rewrite和HTTP模組會用到

zlib  Gzip會用到

OpenSSL  ssl用到

如下命令:

yum install gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

建立目錄(nginx-src),從官方地址(http://nginx.org/)下載,解壓,配置,編譯,安裝:

 mkdir nginx-src && cd nginx-src
 wget http://nginx.org/download/nginx-1.7.3.tar.gz
 tar xzf nginx-1.7.3.tar.gz
 cd nginx-1.7.3
 ./configure
 make
 make install
 whereis nginx
 nginx: /usr/local/nginx

預設的安裝路徑為:/usr/local/nginx;跳轉到/usr/local/nginx/sbin目錄下:

啟動:./nginx
停止:./nginx -s stop

啟動後檢視是否啟動成功,可以在瀏覽器中輸入http://loalhost後有Nginx提示首頁或者輸入如下命令檢視:

ps -ef|grep nginx

類似如下的結果表示成功啟動:

檢視nginx的版本:

/usr/local/nginx/sbin/nginx -v

2、配置Nginx伺服器組

建立兩個nginx-sample-01與nginx-sample-02的Spring Boot服務,然後修改服務埠分別為:8081與8082,並且新增一個簡單的服務類,程式碼如下:

8081埠的nginx-sample-01專案:

@RestController
public class TestCtrl {

    @RequestMapping(value="/")
    public void nginx( ) throws Exception {
    	System.out.println("nginx01---------------");
    }
}

8082埠的nginx-sample-02專案:

@RestController
public class TestCtrl {

    @RequestMapping(value="/")
    public void nginx( ) throws Exception {
    	System.out.println("nginx02---------------");
    }
}

然後打包為jar後,放到CentOS伺服器上。使用如下類似的命令啟動Web服務。

java -jar xxx.jar

現在有兩個Spring Boot對外提供服務,Nginx來做負載均衡。編輯/usr/local/nginx/conf下的nginx.conf檔案,新增upstream配置,這是配置一組被代理的伺服器地址。

 upstream mysvr {
        server 192.168.2.129:8081;
        server 192.168.2.129:8082;
    }

server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://mysvr;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

 }

重啟nginx伺服器後,在瀏覽器中訪問http://localhost,這時候如果有如下輸入,則說明配置生效。

nginx01---------------------
nginx02---------------------
nginx01---------------------
nginx02---------------------
...

nginx預設就是輪詢其權重都預設為1,伺服器處理請求的順序:ABABABABAB....

將nginx新增到系統服務,這樣我們就可以方便的進行常用操作了。

# vi /etc/init.d/nginx

開啟nginx後,新增如下現行程式碼:

# chkconfig: 2345 85 15
# Startup script for the nginx Web Server
# chmod +x /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

成功以後就可以使用如下命令來操作nginx了。

service nginx start/stop/reload..

3、nginx其它負載均衡

(1)ip_hash  nginx中的ip_hash技術能夠將某個ip的請求定向到同一臺後端,這樣一來這個ip下的某個客戶端和某個後端就能建立起穩固的session。但是可能獲取不到正確的ip地址,如nginx之前還有代理,或者nginx後端還有分流的情況,都要考慮。

(2)upstream_hash nginx新版本中可支援讀取cookie值,所以也可以改成 hash   $cookie_jsessionid;假如在php中配置的session為無cookie方式,配合nginx自己的一個userid_module模組就可以用nginx自發一個cookie。

(3)使用nginx sticky實現基於cookie的負載均衡   http://www.ttlsa.com/nginx/nginx-modules-nginx-sticky-module/

(4)一致性hash演算法 可以使用第三方moduler的一致性hash演算法來分配請求。

參考如下博文了解這個演算法:http://www.cnblogs.com/haippy/archive/2011/12/10/2282943.html

github下載:https://github.com/replay/ngx_http_consistent_hash
unzip ngx_http_consistent_hash-master.zip
./configure --add-module=/home/mazhi/Downloads/ngx_http_consistent_hash-master
make
make install

修改mysvr伺服器組,如下:

upstream mysvr {
        consistent_hash $request_uri;
        server 10.10.20.20:8081;
        server 10.10.20.20:8082;
}