1.需求
用戶端發送a.com和b.com兩個不同的請求,經過nginx反向代理伺服器分發到具體的tomcat伺服器,由具體的tomcat伺服器提供服務,如下圖
2.環境準備
nginx作為反向代理伺服器,tomcat作為目标伺服器,Linux下需安裝nginx和tomcat。tomcat執行個體需要兩份,在linux下先安裝一份tomcat,再複制一份,為了區分,需要修改複制的tomcat配置檔案,凡是涉及到端口的如80開頭的都改一下,一台tomcat端口設為8070,另一台設為8080,然後啟動這兩個tomcat測試配置是否成功。為了便于測試,我們在tomcat的webapps下建立兩個頁面。
1.分别tomcat8070和tomc8080目錄下建立myweb檔案夾
mkdir -p ./tomcat8070/webapps/myweb
mkdir -p ./tomcat8080/webapps/myweb
2.index.html檔案
touch ./tomcat8070/webapps/myweb/index.html
touch ./tomcat8080/webapps/myweb/index.html
3.編輯index.html内容 ,tomcat8070的index.html内容為
<html>
<body>
<h1>Welcome to 8070</h1>
<body>
</html>
tomcat8080的index.html内容為
<html>
<body>
<h1>Welcome to 8080</h1>
<body>
</html>
4.啟動兩個tomcat
./tomcat8070/bin/catalina.sh start
./tomcat8080/bin/catalina.sh start
3.nginx反向代理配置
1.修改nginx配置檔案(/usr/local/nginx/conf/nginx.conf),添加兩個server結點。
proxy_pass:配置的是目标伺服器的位址
server{
listen 80;
server_name a.com;
location / {
proxy_pass http://192.168.2.10:8070;
index index.html index.htm;
}
}
server{
listen 80;
server_name b.com;
location / {
proxy_pass http://192.168.2.10:8080;
index index.html index.htm;
}
}
2.啟動nginx
cd /usr/local/nginx/sbin/
./nginx
4.測試
用戶端通過a.com/myweb和b.com/myweb通路,分别會顯示“Welcome to 8070”和“Welcome to 8080”。