Tomcat多个项目部署,通过Nginx反向代理分别配置二级域名的流程

  1. 购买域名。示例:example.com

  2. 设置多个二级域名。如图:

    • Tomcat多个项目部署,通过Nginx反向代理分别配置二级域名的流程
  3. 配置tomcat文件:

    1. 修改tomcat/conf目录下的server.xml文件:
      1. 如下配置配置了3个容器,使用三个不同的端口。
      2. 请注意三点:①端口号:Connector port;②容器名称:portservice name;③项目存放地址:Host appBase;
      3. 示例配置如下,可直接使用。
        <Service name="Catalina">
            <Connector port="8080" protocol="HTTP/1.1"
                       connectionTimeout="20000"
                       redirectPort="8443" />
            <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
            <Engine name="Catalina" defaultHost="localhost">
              <Realm className="org.apache.catalina.realm.LockOutRealm">
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                       resourceName="UserDatabase"/>
              </Realm>
              <Host name="localhost"  appBase="webapps"
                    unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log" suffix=".txt"
                       pattern="%h %l %u %t &quot;%r&quot; %s %b" />
              </Host>
            </Engine>
          </Service>
        
          <Service name="Catalina1">
            <Connector port="8081" protocol="HTTP/1.1"
                       connectionTimeout="20000"
                       redirectPort="8443" />
            <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
            <Engine name="Catalina1" defaultHost="localhost">
              <Realm className="org.apache.catalina.realm.LockOutRealm">
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                       resourceName="UserDatabase"/>
              </Realm>
              <Host name="localhost"  appBase="webapps1"
                    unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log" suffix=".txt"
                       pattern="%h %l %u %t &quot;%r&quot; %s %b" />
              </Host>
            </Engine>
          </Service>
        
          <Service name="Catalina2">
            <Connector port="8082" protocol="HTTP/1.1"
                       connectionTimeout="20000"
                       redirectPort="8443" />
            <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
            <Engine name="Catalina1" defaultHost="localhost">
              <Realm className="org.apache.catalina.realm.LockOutRealm">
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                       resourceName="UserDatabase"/>
              </Realm>
              <Host name="localhost"  appBase="webapps2"
                    unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log" suffix=".txt"
                       pattern="%h %l %u %t &quot;%r&quot; %s %b" />
              </Host>
            </Engine>
          </Service>
    2. 在tomcat根目录下,复制webapps文件夹,并在tomcat根目录下粘贴两份,分别命名为:webapps1和webapps2。注意此文件夹名称需要与第一步server.xml中添加的Host appBase名称一致。示例图如下:
      • Tomcat多个项目部署,通过Nginx反向代理分别配置二级域名的流程
    3. 在tomcat/conf文件夹下,复制Catalina,并在tomcat/conf目录下粘贴两份(Catalina文件夹下的localhost文件夹不要更改名称,否则server.xml所添加的配置也需要改动。为不引起麻烦事,建议直接复制Catalina文件夹即可),分别命名为:Catalina1和Catalina2,注意此文件夹命名需要与第一步server.xml中添加的portservice name名称一致。示例图如下:
      • Tomcat多个项目部署,通过Nginx反向代理分别配置二级域名的流程
  4. 配置Nginx反向代理:修改nginx/conf目录下的nginx.conf文件,添加反向代理配置与虚拟主机配置:

    #反向代理
        upstream www.example.com{
            server ;    #主机ip+端口号
        }
        upstream me.example.com{
            server ;
        }
        upstream dev.example.com{
            server ;
        }
    
    #配置虚拟主机
        server {
            listen ;
            server_name www.example.com;
            index index.jsp index.html index.htm; 
    
            #include proxy-pass-php.conf;
    
            location / {
                    proxy_pass http://www.example.com;    #与server_name保持一致
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
            access_log  /home/wwwlogs/www.example.com.log;
        } 
    
        server {
            listen ;
            server_name me.example.com;
            index index.jsp index.html index.htm; 
    
            #include proxy-pass-php.conf;
    
            location / {
                    proxy_pass http://me.example.com;
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
            access_log  /home/wwwlogs/me.example.com.log;
        } 
    
        server {
            listen ;
            server_name dev.example.com;
            index index.jsp index.html index.htm; 
    
            #include proxy-pass-php.conf;
    
            location / {
                    proxy_pass http://dev.example.com;
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
            access_log  /home/wwwlogs/dev.example.com.log;
        }  
    
    include vhost/*.conf;
  5. 完成配置后,在tomcat中分别于webapps、webapps1、webapps2文件夹下部署三个不同的项目,项目名称都命名为ROOT.war。启动tomcat,分别访问www.example.com、me.example.com、dev.example.com即可。

上一篇:html5 js os build


下一篇:toArray()