[Linux]nginxtomcat做負載平衡

來源:互聯網
上載者:User

[Linux]nginxtomcat做負載平衡

之前使用nginx做過web反向 Proxy,沒有做過負載平衡,今天有個同學需要做tomcat的負載平衡,我也研究下。

一共是2個機器,一個物理機(win7)上面部署2個tomcat,使用不同的連接埠啟動。vm中的虛擬機器放(centos)nginx,給tomcat做負載平衡.
inux ip: 192.168.37.129
win ip: 192.168.37.1

首先保證兩個主機可以互ping,響應的連接埠開放。

nginx上使用80
tomcat1 使用8081 tomcat2使用8080
nginx,tomcat的安裝和啟動不在說了,特別是windows下很簡單,遇到困難百度下就可以了。
windows安裝啟動個tomcat 還有 一個系統啟動多個tomcat 為了區分每個tomcat的不同,把首頁的html加上些標誌。
nginx版本1.4 tomcat 版本6.0
2個tomcat執行個體可以正常啟動,nginx執行個體也能正常使用,win上的瀏覽器中可以正常訪問3個網頁
http://192.168.37.1:8081/
http://192.168.37.1:8080/
http://192.168.37.129/

然後開始配置nginx:

[root@localhost conf]# cat /etc/nginx/nginx.conf

#user  nobody;worker_processes  1;events {    worker_connections  1024;} http {    include       mime.types;    default_type  application/octet-stream;     upstream localhost{        server 192.168.37.1:8080 weight=2;        server 192.168.37.1:8081 weight=1;        #ip_hash;    }     sendfile        on;    keepalive_timeout  65;     server {        listen       80;        server_name  localhost;         location / {            proxy_connect_timeout  3;            #proxy_redirect        off;            proxy_send_timeout     30;            proxy_read_timeout     30;            proxy_pass      http://localhost;        }         error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}

這怎麼重啟nginx都沒生效,最後發現是因為啟動的設定檔不對,可以通過 nginx -c /etc/nginx/nginx.conf 來指定陪設定檔的路徑。

當然這裡是最簡單的輪詢,沒有其他策略,實驗來說基本成功。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.