Nginx+Tomcat搭建高效能負載平衡叢集-Windows本地測試版

來源:互聯網
上載者:User

標籤:body   nop   機率   處理   add   cache   .htaccess   with   script   

一、安裝Tomcat和Nginx

首先安裝兩個apache-tomcat-8.0.41,:http://tomcat.apache.org

並安裝一個nginx-1.13.0,http://nginx.org/en/download.html

都是綠色版,直接解壓就能用,不需要進行環境變數之類的配置的。

這裡碰到個小問題:公司電腦環境變數配的是jdk6,所以我的Tomcat8啟動的時候黑視窗一閃而過,JDK版本不匹配的原因,把jdk6換成jdk8之後,Tomcat8正常啟動。

二、修改Tomcat的連接埠號碼

我的第一個Tomcat是預設的,只修改第二個Tomcat,確保兩個Tomcat能同時啟動,需要修改下面三處的連接埠號碼(O(∩_∩)O我只是在預設多口號加了1~):

三、修改Tomcat的預設頁面,用於識別訪問的是哪個Tomcat

 另一個Tomcat這裡添加的是=====Tomcat1

四、修改Nginx的配置

配置內容

  1 #user  nobody;  2 worker_processes  1;#背景工作處理序的個數,一般與電腦的cpu核心數一致  3   4 #error_log  logs/error.log;  5 #error_log  logs/error.log  notice;  6 #error_log  logs/error.log  info;  7   8 #pid        logs/nginx.pid;  9  10  11 events { 12     worker_connections  1024;#單個進程最大串連數(最大串連數=串連數*進程數) 13 } 14  15  16 http { 17     include       mime.types;#副檔名與檔案類型映射表 18     default_type  application/octet-stream;#預設檔案類型 19  20     #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘ 21     #                  ‘$status $body_bytes_sent "$http_referer" ‘ 22     #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘; 23  24     #access_log  logs/access.log  main; 25  26     sendfile        on;#開啟高效檔案傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出檔案, 27                #對於普通應用設為 on,如果用來進行下載等應用磁碟IO重負載應用,可設定為off, 28                #以平衡磁碟與網路I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。 29     #tcp_nopush     on; 30  31     #keepalive_timeout  0; 32     keepalive_timeout  65;#長連線逾時時間,單位是秒 33  34     #gzip  on;#啟用Gizp壓縮 35  36     #伺服器的叢集   37     upstream  netitcast.com {  #伺服器叢集名字    38         server    127.0.0.1:8080  weight=1;#伺服器配置   weight是權重的意思,權重越大,分配的機率越大。   39         server    127.0.0.1:8081  weight=2;   40     }  41  42     #當前的Nginx的配置 43     server { 44         listen       80;#監聽80連接埠,可以改成其他連接埠 45         server_name  localhost;#當前服務的網域名稱 46  47         #charset koi8-r; 48  49         #access_log  logs/host.access.log  main; 50  51         location / { 52             root   html; 53             index  index.html index.htm; 54         proxy_pass http://netitcast.com;   55             proxy_redirect default; 56         } 57  58         #error_page  404              /404.html; 59  60         # redirect server error pages to the static page /50x.html 61         # 62         error_page   500 502 503 504  /50x.html; 63         location = /50x.html { 64             root   html; 65         } 66  67         # proxy the PHP scripts to Apache listening on 127.0.0.1:80 68         # 69         #location ~ \.php$ { 70         #    proxy_pass   http://127.0.0.1; 71         #} 72  73         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 74         # 75         #location ~ \.php$ { 76         #    root           html; 77         #    fastcgi_pass   127.0.0.1:9000; 78         #    fastcgi_index  index.php; 79         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 80         #    include        fastcgi_params; 81         #} 82  83         # deny access to .htaccess files, if Apache‘s document root 84         # concurs with nginx‘s one 85         # 86         #location ~ /\.ht { 87         #    deny  all; 88         #} 89     } 90  91  92     # another virtual host using mix of IP-, name-, and port-based configuration 93     # 94     #server { 95     #    listen       8000; 96     #    listen       somename:8080; 97     #    server_name  somename  alias  another.alias; 98  99     #    location / {100     #        root   html;101     #        index  index.html index.htm;102     #    }103     #}104 105 106     # HTTPS server107     #108     #server {109     #    listen       443 ssl;110     #    server_name  localhost;111 112     #    ssl_certificate      cert.pem;113     #    ssl_certificate_key  cert.key;114 115     #    ssl_session_cache    shared:SSL:1m;116     #    ssl_session_timeout  5m;117 118     #    ssl_ciphers  HIGH:!aNULL:!MD5;119     #    ssl_prefer_server_ciphers  on;120 121     #    location / {122     #        root   html;123     #        index  index.html index.htm;124     #    }125     #}126 127 }

其實也就修改了以下兩個地方:

五、開始測試

通過startup.bat直接啟動兩個Tomcat。會出現兩個黑視窗,裡面有Tomcat的開機記錄。

接著通過Nginx目錄下的nginx.exe啟動Nginx。

六、測試結果

如所示,瀏覽器中訪問localhost,不斷重新整理,訪問的Tomcat會在1和2中來回切換,切換的機率是由所配置的權重決定的。

 

Nginx+Tomcat搭建高效能負載平衡叢集-Windows本地測試版

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.