圖文解說:Nginx + tomcat配置集羣負載均衡

        開發的應用採用F5負載均衡交換機,F5將請求轉發給5臺hp unix服務器,每臺服務器有多個webserver實例,對外提供web服務和socket等接口服務。之初,曾有個小小的疑問爲何不採用開源的apache、Nginx軟件負載,F5設備動輒幾十萬,價格昂貴?自己一個比較幼稚的問題,後續明白:F5是操作於IOS網絡模型的傳輸層,Nginx、apache是基於http反向代理方式,位於ISO模型的第七層應用層。直白些就是TCP UDP 和http協議的區別,Nginx不能爲基於TCP協議的應用提供負載均衡。

   瞭解了二者之間的區別於應用場景,對Nginx產生濃厚的興趣,閱讀張宴的<實戰Nginx>(這個85年的小夥子年輕有爲羨慕+妒忌),搞明白了大致原理和配置,Ubuntu10.10,window下對Nginx+tomcat負載均衡做了配置嘗試,將全部請求轉發到tomcat,並未做靜態,動態分開,圖片防盜鏈等配置。

Nginx 介紹

       Nginx (發音同 engine x)是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。  其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁伺服器中表現較好.目前中國大陸使用nginx網站用戶有:新浪、網易、 騰訊,另外知名的微網誌Plurk也使用nginx。

      上面的全是Nginx介紹基本上是廢話,下面轉入正題,圖文結合展示基本配置,首先是window環境、其次是Ubuntu環境(Vbox虛擬)。本文主要基於Nginx下配置兩臺tomcat,結構如下圖:

Window xp環境:Nginx + Tomcat6

1、下載地址

http://nginx.org/en/downlaod.xml,這裏我們推薦下載穩定版(stable versions),本文采用nginx-1.4.6。

2、目錄結構

     window下安裝Nginx極其簡單,解壓到一個無空格的英文目錄即可(個人習慣,擔心中文問題),雙擊nginx,這裏我安裝到:D:\server目錄,下面涉及到的tomcat也安裝在此目錄。

   

Dos環境啓動:默認啓用80端口

訪問歡迎html頁

在瀏覽器中訪問http://locahost,可以看到默認的歡迎頁面。

若想停止nginx,dos環境運行命令:nginx -s stop

3、nginx.conf配置

Nginx配置文件默認在conf目錄,主要配置文件爲nginx.conf,我們安裝在D:\server\nginx-1.4.6\、默認住配置文件爲D:\server\nginx-1.4.6\conf\nginx.conf。下面是nginx作爲前端反向代理服務器的配置。

nginx.conf代碼需要做如下修改:

1)、在#gzip  on;後面加入下面的配置:

upstream backend{
            server localhost:8080;
            server localhost:8088;
            ip_hash;
    }

其中server localhost:8080爲第一個Tomcat的啓動地址,server localhost:8088爲第二個Tomcat的啓動地址,ip_hash用於做session同步。

2)、修改第一個server{}配置中的listen 80改爲新的端口號,因爲我的本機80端口被IIS佔用,因此將此處改爲listen 800。並將

          location/{

                         root html;

                         index index.html index.htm;

改爲:

          server {
        listen       800;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass   http://backend;
            proxy_redirect  off;
            proxy_set_header  Host $host;
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size  10m;
            client_body_buffer_size  128k;
            proxy_connect_timeout  90;
            proxy_send_timeout  90;
            proxy_read_timeout  90;
            proxy_buffer_size  4k;
            proxy_buffers  4 32k;
            proxy_busy_buffers_size  64k;
            proxy_temp_file_write_size  64k;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    其中proxy_pass參數和upstream backend{}對應。

    下面先啓動兩臺tomcat,然後雙擊nginx根目錄下的nginx.exe文件或者用start nginx,打開瀏覽器,輸入地址:http://localhost:800,便可看到下面的界面:

    








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章