windows下nginx+tomcat分布式集群部署


 

首先官網下載  http://nginx.org/en/download.html,我的本地環境為

實現的架構:

 

從圖上可以看出,nginx作為負載均衡請求分發器,當請求A應用時候,分發到A集群,同理當請求B應用的時候,分發到B集群。

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


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  off;  
    access_log  logs/access.log;  
  
    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  75 20;  
  
    include    gzip.conf;
    upstream localhost {  
      #根據ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實並不能。  
      #同一機器在多網情況下,路由切換,ip可能不同  
      #ip_hash;   
      server localhost:18001;  
      server localhost:18002;
      server localhost:18003;  
      server localhost:18004;    
     } 
     upstream t1 {  
      server localhost:18001;  
     }   
      upstream t2{  
     
      server localhost:18002;  
     }   
     
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
         #   root   html;
         #   index  index.html index.htm;
         proxy_connect_timeout   3;  
         proxy_send_timeout      30;  
         proxy_read_timeout      30;  
         proxy_pass http://localhost;
        }

        location /t1.jsp {
         #   root   html;
         #   index  index.html index.htm;
         proxy_connect_timeout   3;  
         proxy_send_timeout      30;  
         proxy_read_timeout      30;  
         proxy_pass http://t1;
        }
       location /t2.jsp {
         #   root   html;
         #   index  index.html index.htm;
         proxy_connect_timeout   3;  
         proxy_send_timeout      30;  
         proxy_read_timeout      30;  
         proxy_pass http://t2;
        }
        #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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

  Proxy.conf

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   300;  
proxy_send_timeout      300;  
proxy_read_timeout      300;  
proxy_buffer_size       4k;  
proxy_buffers           4 32k;  
proxy_busy_buffers_size 64k;  
proxy_temp_file_write_size 64k;  

  Gzip.conf

gzip              on;  
gzip_min_length      1000;  
gzip_types         text/plain text/css application/x-javascript;

  tomcat因為是只有一台主機,server.xml將端口號全部改為不同的。

 

這個地方分別每台tomcat加上標識tomcat1,tomcat2,tomcat3,tomcat4

然后分別在D:\nginxtomcat\apache-tomcat-6.0.18_1\webapps\ROOT,我本地環境路徑下,寫個jsp文件,內容分別是tomcat1,tomcat2,tomcat3,tomcat4。。。用來測試nginx分發

 

jsp內容:

 

 tomcat端口號修改完,以及加上jsp文件以后,就可以測試了。

啟動nginx:

nginx -t 測試nginx.conf文件,如果有錯誤,會提示比如下面的錯誤

start nginx。以后台運行nginx的方式啟動(推薦用這種方式)

分別啟動tomcat1,tomcat2,tomcat3,tomcat4。。。用來測試nginx分發

 

訪問http://localhost/hello.jsp

頁面依次出現tomcat1,2,3,內容。證明成功了。。。

因為nginx默認按輪訓的方式依次分發的,如果想改變負載方式,修改這里

關於upstream:

 

1、輪詢(默認)

每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。

2、weight
指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。
例如:

upstream bakend {
server 192.168.159.10 weight=10;
server 192.168.159.11 weight=10;
}

 


3、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
例如:

upstream resinserver{
ip_hash;
server 192.168.159.10:8080;
server 192.168.159.11:8080;
}


4、fair(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。

upstream resinserver{
server server1;
server server2;
fair;
}


5、url_hash(第三方)

按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。

例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法


upstream resinserver{
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

tips:

 

upstream resinserver{#定義負載均衡設備的Ip及設備狀態
ip_hash;
server 127.0.0.1:8000 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6801;
server 127.0.0.1:6802 backup;
}

在需要使用負載均衡的server中增加

proxy_pass http://resinserver/;


每個設備的狀態設置為:
1.down 表示單前的server暫時不參與負載
2.weight 默認為1.weight越大,負載的權重就越大。
3.max_fails :允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
4.fail_timeout:max_fails次失敗后,暫停的時間。
5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。

nginx支持同時設置多組的負載均衡,用來給不用的server來使用。

client_body_in_file_only 設置為On 可以講client post過來的數據記錄到文件中用來做debug
client_body_temp_path 設置記錄文件的目錄 可以設置最多3層目錄
location 對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡

 

 

到此結束了,在生產環境下,一台nginx肯定是不夠完美的,如果nginx掛掉,是不是整個應用就down了。。。生產環境下,我們可以部署多台nginx,利用f5或者lvs+keepalive來實現負載。

f5是傳輸層負載。

nginx是應用層負載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM