1. 程式人生 > >Nginx+tomcat叢集環境搭建

Nginx+tomcat叢集環境搭建

實驗環境:windows xp sp3

Nginx版本:1.5.12;

Tomcat版本:6.0.39

一、配置nginx

1、  在D盤根目錄建立nginx資料夾,把下載的nginx釋出包nginx-1.5.12.zip解壓到該目錄。

2、  D:\nginx\nginx-1.5.12目錄結構

Nginx-

              |_  conf      配置目錄

              |_  contrib.

              |_  docs      文件目錄

              |_  logs      日誌目錄
              |_  temp      臨時檔案目錄

              |_  html      靜態頁面目錄

              |_  nginx.exe 主程式

3、  啟動nginx

windows下啟動nginx非常簡單,雙擊執行nginx.exe即可。Nginx預設執行在80埠,檢查nginx是否啟動我們只需要在瀏覽器中輸入http://localhost便可看到如下頁面,說明我們nginx已經啟起來了。

1、  停止nginx

如果需要停止nginx,需要開啟一個命令列視窗,進入nginx解壓的目錄,也就是進入nginx.exe檔案所在的目錄,輸入命令nginx –s stop 便可停止nginx。

二、叢集配置

1、  配置tomcat

在D盤根目錄建立tomcat資料夾,解壓2份tomcat6.0.39釋出包到該目錄下,分別命名為tomcat01,tomcat02。為了便於觀察我們訪問的是哪個tomcat,我們修改tomcat01的D:\tomcat\tomcat01\webapps\ROOT\index.html中

<td align="left" valign="top"><b>Apache Tomcat</b></td>

改為

<td align="left" valign="top"><b>Apache Tomcat 1</b></td>

同理我們把tomcat02的D:\tomcat\tomcat02\webapps\ROOT\index.html中

<td align="left" valign="top"><b>Apache Tomcat</b></td>

改為

<td align="left" valign="top"><b>Apache Tomcat 2</b></td>

2個tomcat我們在同一臺計算機上,為了讓2個tomcat的埠不衝突,我們把tomcat02的D:\tomcat\tomcat02\conf\server.xml中

<Server port="8005" shutdown="SHUTDOWN">

改為

<Server port="8105" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />

改為

<Connector port="8180" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8543" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

改為

<Connector port="8109" protocol="AJP/1.3" redirectPort="8543" />

2、  配置nginx

nginx.conf

#Nginx所用使用者和組,window下不指定 
#user  niumd niumd; 

#工作的子程序數量(通常等於CPU數量或者2倍於CPU)
worker_processes  2;

#錯誤日誌存放路徑
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定pid存放檔案
#pid        logs/nginx.pid;


events {
    #使用網路IO模型linux建議epoll,FreeBSD建議採用kqueue,window下不指定。
    #use epoll;
    
    #允許最大連線數
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #定義日誌格式 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    
    client_header_timeout  3m;
    client_body_timeout    3m;
    send_timeout           3m;
    
    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;
    

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    upstream localhost {  
	      #根據ip計算將請求分配各那個後端tomcat,許多人誤認為可以解決session問題,其實並不能。  
	      #同一機器在多網情況下,路由切換,ip可能不同  
	      ip_hash;   
	      server localhost:8080;  
	      server localhost:8180;  
	     }  


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_connect_timeout   3;  
            proxy_send_timeout      30;  
	          proxy_read_timeout      30;  
            proxy_pass http://localhost;  
        }

        #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;
        }

    }
}

3、  檢視反向代理配置結果

啟動nginx、tomcat01、tomcat02。

瀏覽器輸入http://localhost便看到tomcat01的管理介面,如下圖。

然後停止tomcat01,重新整理頁面,nginx自動幫我們切換到tomcat02了,如下圖。