实验环境:CentOS7

前提:已经安装好tomcat,未安装请查看http://www.cnblogs.com/wzhuo/p/7111135.html

目的:基于主机名访问两个应用;

[root@tomcat localhost]#cd /etc/tomcat/
[root@tomcat localhost]#vim server.xml
#定义连接器的地址和端口、连接超时时长、最大并发数、是否反解ip成主机名
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"  address="172.16.252.142" maxThreads="200" enableLookups="false"
               redirectPort="8443" />
#定义host虚拟主机:文件路径,是否直接解压wars文件,是否支持自动重载
 <Host name="linux1.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
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>
      <Host name="linux2.com"  appBase="/data/apps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="linux2_access_log." suffix=".log"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
#linux1.com的文件路径
[root@tomcat localhost]#cd /usr/share/tomcat/webapps/
[root@webapps localhost]#ls
examples  host-manager  manager  ROOT  sample  shopxx  shopxx-3.0Beta

#linux2的文件路径:
[root@webapps localhost]#cd /data/apps/
[root@apps localhost]#ls
test
[root@apps localhost]#ls test/
classes  index.jsp  lib  META-INF  WEB-INF
[root@apps localhost]#cat test/index.jsp
<%@ page language="java" %>
 <html>
         <head><title>TomcatA</title></head>
  <body>
       <h1><font color="red">TomcatA.magedu.com</font></h1>
        <table align="centre" border="1">
         <tr>
           <td>Session ID</td>
              <% session.setAttribute("magedu.com","magedu.com"); %>
                    <td><%= session.getId() %></td>
               </tr>
          <tr>
                 <td>Created on</td>
                  <td><%= session.getCreationTime() %></td>
                      </tr>
            </table>
     </body>
   </html>

#测试:

 

 

 #tomcat和Nginx部署在同一主机,Nginx只做反代:

[root@~ localhost]#yum -y install nginx-1.10.2-1.el7.ngx.x86_64.rpm
[root@~ localhost]#vim /etc/nginx/conf.d/default.conf 
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
#如果此处写的是主机名,则务必在/etc/hosts文件中进行配置
        proxy_pass http://172.16.254.47:8080;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }


[root@~ localhost]#nginx -t
[root@~ localhost]#systemctl start nginx 

#tomcat只处理动态页面:

[root@~ localhost]#vim /etc/nginx/conf.d/default.conf 
server {
    listen       80;
    server_name  localhost;

    location / {
#页面资源的路径
        root   /usr/share/tomcat/webapps/ROOT;
#默认主页;不用在浏览器键入index.jsp
        index   index.jsp index.html index.htm;
    }
#将动态资源反代
    location ~* \.(jsp|do)$  {
        proxy_pass http://172.16.254.47:8080;
        index  index.jsp index.html;
    }
[root@~ localhost]#nginx -t
[root@~ localhost]#nginx -s reload

 

#httpd做反代:

#httpd只做反代
#使用的是proxy_http_module模块
[root@conf.d localhost]#cd /etc/httpd/conf.d/
[root@conf.d localhost]#vim tomcat-http.conf
<VirtualHost *:80>
                ServerName      172.16.254.47
#关闭正向代理
                ProxyRequests Off
#代理服务器地址经由头部处理
                ProxyVia        On
#启用反向代理
                ProxyPreserveHost On
                <Proxy *>
                    Require all granted
                </Proxy>
                ProxyPass / http://172.16.254.47:8080/
                ProxyPassReverse / http://172.16.254.47:8080/ 
                <Location />
                    Require all granted
                </Location>
            </VirtualHost>

[root@conf.d localhost]#httpd -t
[root@conf.d localhost]#systemctl start httpd.setvice
#用proxy_ajp_module模块代理时,只需将http换成ajp,8080改成8009即可

 #测试