1. 程式人生 > >apt-get安裝tomcat並測試nginx負載均衡

apt-get安裝tomcat並測試nginx負載均衡

ubuntu nginx tomcat

1、tomcat安裝

參考我的另一篇文章

ubuntu安裝tomcat7


2、多個tomcat的配置

編輯/etc/tomcat7/service.xml,配置字段Service name 、Connect port、defaulthost、Host name、appBase、prefix

下圖分別配置端口分別為8080、8081、8082

其中我刪除了大部分註釋的內容

技術分享圖片


技術分享圖片


技術分享圖片


3、準備測試頁面

cd /var/lib/tomcat7


sudo cp -r webapps webapps8081

sudo cp -r webapps webapps8081


sudo chown tomcat7:tomcat7 webapps8081

sudo chown tomcat7:tomcat7 webapps8082


sudo vi /var/lib/tomcat/webapps/ROOT/index.html

修改字段<h1>It works 8080!</h1>


sudo vi /var/lib/tomcat/webapps8081/ROOT/index.html

修改字段<h1>It works 8081!</h1>


sudo vi /var/lib/tomcat/webapps8082/ROOT/index.html

修改字段<h1>It works 8082!</h1>


啟動tomcat

sudo service tomcat7 start


4、瀏覽器訪問測試

如下圖所示

技術分享圖片


技術分享圖片


技術分享圖片


5、安裝配置nginx

安裝

sudo apt-get install nginx


nginx配置

sudo vi /etc/nginx/nginx.conf

在http {}中添加如下圖所示內容

技術分享圖片


檢查配置文件

sudo nginx -t


啟動

sudo nginx


檢查端口

ps -ef | grep nginx


netstat -an | grep :18080


這裏需要註意的是nginx服務端口不要按照默認的80,因為如果按照默認的端口,則無法跳轉到tomcat的頁面,一直顯示的是nginx的默認頁面,具體原因還未找到。


6、瀏覽器訪問測試

http://localhost:18080


刷新頁面發現頁面在變化

技術分享圖片


技術分享圖片


技術分享圖片


參考:

http://blog.51cto.com/purplegrape/932811

https://blog.csdn.net/dylanzr/article/details/52135196


apt-get安裝tomcat並測試nginx負載均衡