1. 程式人生 > >nginx+tomcat實現單個IP地址,多個二級域名+多個站點訪問

nginx+tomcat實現單個IP地址,多個二級域名+多個站點訪問

1.部署多臺tomcat

  簡單的在一臺伺服器上部署多臺tomcat最簡單不過了,只需要將tomcat壓縮包解壓到多個目錄,然後更改/conf/server.xml配置檔案中的三個埠:

  a.Server port=”8[X]05″ shutdown=”SHUTDOWN”

  b.Connector port=”8[X]80″ maxHttpHeaderSize=”8192″ …

  c.Connector port=”8[X]09″ enableLookups=”false”

  這裡用‘X’代替第幾臺tomcat,預設的分別為8005,8080,8009。每增加一臺tomcat只需要將這三個數字遞增就可以。這樣就可以根據IP+8[X]80來區別不同的程式了。

2.用nginx繫結域名與不同的tomcat埠

  nginx配置檔案位於conf/nginx.conf,它最主要的部分是http部分,這裡最重要的兩個配置項是upstream,server,這兩個項都可以有多個配置。

在http{}內插入下面程式碼。

  1. upstream home.console.xinyi8090.cn {    
  2.             server 60.205.149.58:8082;        
  3.     }    
  4.     upstream home.vendor.xinyi8090.cn {    
  5.             server 60.205.149.58:8080;  
  6.     }  
  7.     server {  
  8.         listen       80;  
  9.         server_name  home.console.xinyi8090.cn;  
  10.         location / {  
  11.             index  index.html index.jsp;      
  12.             proxy_pass  http://home.console.xinyi8090.cn;      
  13.             proxy_set_header    X-Real-IP   $remote_addr;      
  14.             client_max_body_size    100m;   
  15.         }  
  16.     }  
  17.     server {  
  18.         listen       80;  
  19.         server_name home.vendor.xinyi8090.cn;  
  20.         location / {  
  21.             index  index.html index.jsp;      
  22.             proxy_pass  http://home.vendor.xinyi8090.cn;      
  23.             proxy_set_header    X-Real-IP   $remote_addr;      
  24.             client_max_body_size    100m;   
  25.         }  
  26.     }
大功告成,親測有效。

3.擴充套件修改tomcat預設頁

  很多時候我們想輸入域名之後就可以訪問主頁,這時候就需要配置tomcat的預設頁面。

首先,修改$tomcat/conf/server.xml檔案。
在<host></host>標籤之間新增上:

<Context path="" docBase="mypr" debug="0" reloadable="true" />


path是說明虛擬目錄的名字,如果你要只輸入ip地址就顯示主頁,則該鍵值留為空;


docBase是虛擬目錄的路徑,它預設的是$tomcat/webapps/ROOT目錄,現在我在webapps目錄下建了一個mypro專案,讓該專案檔案作為我的預設目錄。

然後,修改$tomcat/conf/web.xml檔案。
在web.xml檔案中,有一段如下:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

改成mypro專案中需要直接顯示的jsp或者html即可。