Nginx+Tomcat集羣搭建

      上篇博客寫了nginx作爲反向代理的作用,它能作爲一個收集器,收集客戶端請求,然後分發給集羣的服務器處理。這種方式降低了訪問壓力。今天介紹怎麼在windows下搭建集羣(nginx+2個tomcat)。

一、準備工作

1.Nginx下載(我用的nginx-1.4.7)

2.tomcat(我用的6.0.28)

二、配置

1.首先修改nginx的配置文件,在conf/nginx.conf下的文件。

#服務器的集羣
    upstream  tomcat.com {  #服務器集羣名字
  		#server   172.16.21.13:8081 weight=1;#服務器配置   weight是權重的意思,權重越大,分配的概率越大。
		#server   192.168.1.186:8081 weight=1;
		#server   172.16.1.14:8081 weight=2;
		#server   172.16.1.15:8081 weight=1;
		#server   172.16.1.15:80 weight=1;		
		server    127.0.0.1:18081  weight=1; #自己配置
		server    127.0.0.1:18082  weight=2; 
	}	

	#當前的Nginx的配置
    server {
        listen       80;#監聽80端口,可以改成其他端口
        server_name  localhost;##############	當前服務的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

	location / {
            proxy_pass http://tomcat.com; //和upstream的名字相同
            proxy_redirect default;
        }
		location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$
        {
         
            expires 30d;
             root /nginx-1.4.7;#root:
            break;
        }


主要要修改這些部分,配置監聽的tomcat的端口和權重

2.修改tomcat配置文件

 在conf下的server.xml文件

<Server port="18005" shutdown="SHUTDOWN"> #端口是18005,兩個tomcat不能重複,否則啓動失敗,隨便配
  <Connector port="18081" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" /> #端口18081,和nginx的配置文件裏面監聽的tomcat的端口一致

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">  //加上jvmRoute="tomcat1"

另一個tomcat的配置大致相同

三、啓動測試

1.先啓動nginx

 切換到nginx的目錄E:\nginx+tomcat\nginx-1.4.7,然後在命令窗口輸入:start nginx.exe,想要確定是否啓動成功,輸入nginx -t查看狀態

 


 分別啓動兩個tomcat,這裏我修改了tomcat的默認頁面的內容,路徑在E:\nginx+tomcat\apache-tomcat1\webapps\ROOT下的index.jsp。啓動成功後,在頁面中輸入地址:http://localhost/index.jsp,就能訪問了 。如下:


 再訪問就可能調到這個頁面,注意兩個頁面

  因爲在nginx的配置文件中設置了權重,那麼訪問頁面的次數也會按照權重分配。

四、總結

    大家感興趣可以搭建一下特別簡單,nginx在解決集羣和高併發性上應用很高,而且跨平臺配置簡單。


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