posts - 36, comments - 30, trackbacks - 0, articles - 3

          Ngnix+Tomcat配置負(fù)載均衡

          Posted on 2016-01-28 19:45 笑看人生 閱讀(3280) 評論(0)  編輯  收藏 所屬分類: Web開發(fā)技術(shù)
          @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

          1.安裝tomcat

          下載對應(yīng)的Tomcat版本apache-tomcat-7.0.67-windows-x86.zip,解壓到D:\apache-tomcat-7.0.67,配置如下環(huán)境變量:

          CATALINA_HOME    D:\apache-tomcat-7.0.67

          Path                  %CATALINA_HOME%\bin;

          運行D:\apache-tomcat-7.0.67\bin\startup.bat命令啟動Tomcat

           

          打開瀏覽器,輸入http://localhost:8080/,如果能正常顯示如下頁面,說明Tomcat配置成功。



          2.安裝Ngnix

          下載對應(yīng)的Ngnix版本  nginx-1.6.3.zip,解壓到D:\nginx-1.6.3

          Ngnix的默認(rèn)監(jiān)聽端口是80,修改默認(rèn)監(jiān)聽端口為88(不修改也可以,只是本機的80端口已被其他服務(wù)占用了)

          修改配置文件D:\nginx-1.6.3\conf\nginx.conf

                 server {

                  listen       88; 

              ….

          }

          運行D:\nginx-1.6.3\nginx.exe

          瀏覽器中輸入http://localhost:88/,如果能打開如下頁面,說明nginx配置成功。



          配置nginx,讓nginx反向代理tomcat

          修改配置文件D:\nginx-1.6.3\conf\nginx.conf

              server {

                  listen       88;   #80 ==> 88

                  server_name  localhost; 

           

                  #charset koi8-r;

           

                  #access_log  logs/host.access.log  main;

           

                  location / {

                      proxy_pass http://localhost:8080;

                      #root   html;

                      #index  index.html index.htm;

                    }

                     

          }

          運行nginx -s reload重新加載ngnix的配置文件,然后啟動Tomcat

          在瀏覽器中輸入http://localhost:88/,如果能打開Tomcat的主頁面,說明ngnix反向代理Tomcat配置成功。


          配置讓Tomcat處理所有jsp頁面

                location ~ \.jsp$ {  # / ==> ~ \.jsp

                      proxy_pass http://localhost:8080;

                      #root   html;

                      #index  index.html index.htm;

                  }

           

          運行nginx -s reload重新加載ngnix的配置文件

          在瀏覽器中輸入http://localhost:88/,發(fā)現(xiàn)這時候顯示如下頁面



          因為請求地址沒有以jsp結(jié)尾,所以沒有轉(zhuǎn)發(fā)到Tomcat,如果把地址改成http://localhost:88/index.jsp,這時候會顯示如下頁面



          發(fā)現(xiàn)頁面雖然顯示成功了,但是頁面上的圖片和css樣式?jīng)]有顯示出來,因為我們沒有配置讓ngnix來處理這些靜態(tài)頁面元素,使用ngnixtomcat的一個主要功能就是做到動靜分離,即讓tomcat來處理像jsp之類的動態(tài)頁面,讓ngnix來處理htmljpg等之類的靜態(tài)頁面。我們在配置文件中追加如下配置

                 location ~ \.(html|js|css|png|gif)$ {

                      root   D:\apache-tomcat-7.0.67\webapps\ROOT;

                  }

           

          運行nginx -s reload重新加載ngnix的配置文件

           

          再輸入http://localhost:88/index.jsp,這時候會正常顯示如下頁面



          配置第二個Tomcat務(wù)

          解壓apache-tomcat-7.0.67-windows-x86.zip到目錄 C:\apache-tomcat-7.0.67,配置如下環(huán)境變量:

          CATALINA2_HOME    C:\apache-tomcat-7.0.67

          Path                  %CATALINA2_HOME%\bin;

           

          為了避免和第一個Tomcat的端口沖突,修改Tomcat如下端口,打開C:\apache-tomcat-7.0.67\conf\server.xml文件,修改如下端口

           

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

           

          <Connector port="18080" protocol="HTTP/1.1"

                         connectionTimeout="20000"

                         redirectPort="18443" />

           

          <Connector port="18009" protocol="AJP/1.3" redirectPort="18443" />

           

          修改startup.batcatalina.bat和shutdown.bat文件內(nèi)容:

           

          (1) 打開C:\apache-tomcat-7.0.67\conf\startup.bat文件,把其中所有CATALINA_HOME換為CATALINA2_HOME

          (2)打開C:\apache-tomcat-7.0.67\conf\catalina.bat文件,把其中所有CATALINA_HOME換為CATALINA2_HOME

          (3)打開C:\apache-tomcat-7.0.67\bin\shutdown.bat文件,把其中所有CATALINA_HOME換為CATALINA2_HOME


          在地址欄中輸入http://localhost:18080/,如果能正常顯示如下頁面,說明配置成功。



           

          配置ngnix,讓它可以反向代理兩臺Tomcat實現(xiàn)負(fù)載均衡。

          修改配置文件D:\nginx-1.6.3\conf\nginx.conf

              upstream local_tomcats {

                 server localhost:8080;

                 server localhost:18080;

               }

          server {

                  listen       88;   #80 ==> 88

                       server_name  localhost;

          location ~ \.jsp$ {  # / ==> ~ \.jsp

                      proxy_pass http://local_tomcats;

                      #root   html;

                      #index  index.html index.htm;

                  }

          }


          ##注意:如果Tomcat分布在不同的機器上,需要在防火墻中開通對應(yīng)的端口 8080或18080

          @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
          主站蜘蛛池模板: 香河县| 积石山| 南皮县| 德清县| 天津市| 扶绥县| 敦化市| 浪卡子县| 尖扎县| 光山县| 定结县| 庐江县| 贞丰县| 陇南市| 讷河市| 磴口县| 溧水县| 鲜城| 开原市| 健康| 新宁县| 万载县| 临漳县| 拉萨市| 凭祥市| 滦平县| 平利县| 江阴市| 盈江县| 于都县| 江达县| 临沭县| 怀柔区| 宿州市| 都江堰市| 新民市| 巫山县| 平昌县| 平凉市| 庆元县| 镇远县|