• 当前位置: 主页 > Linux安装教程 > 系统设置 >

    apache反向代理tomcat_tomcat配置及基于nginx、apache反向代理tomcat

    时间:2016-06-11 22:43来源:未知 作者:Linux先生 举报 点击:
    湖南成人高考 成人高考 成人高考 成人高考  成人高考 成人高考 
    pache HTTP server 和 Tomcat server 整合,一般是希望对于用户只公布 Apache HTTP server 的网址,而 Tomcat 的网址则不公布,扮演一个幕后英雄的角色。

     公司添加项目,需要架设环境,当时考虑用的nginx转发tomcat,可是分公司那边不会用nignx,只有用apache 反向代理 tomcat 虚拟主机,由于是头一次用 apache 做反向,tomcat集群,特留下过程,方便他人在网上找了好多文章,其中为经典的我都摘抄了

         Apache HTTP server 和 Tomcat server 整合,一般是希望对于用户只公布 Apache HTTP server 的网址,而 Tomcat 的网址则不公布,扮演一个幕后英雄的角色。访问Tomcat 的 HTTP 请求,通过 Apache 转发给 Tomcat,Tomcat 处理完后,将 HTTP 回应返回给 Apache,然后 Apache  HTTP 回应发回给用户端浏览器。
    Apache HTTP server 和 Tomcat server 直接的 HTTP 数据传输,有很多种方法。
         
    方法一,使用 mod_jk。很多网站上介绍到 Apache HTTP server 和 Tomcat server 整合的时候,都是在介绍  mod_jk.so 的使用,这是一种比较老的方法,并且需要额外下载mod_jk。Apache 和 Tomcat 的默认配置文件都需要改动。
    方法二, URL rewrite,也就是对于指定格式的 URL,转发给某个 Tomcat 的网址。这里所说的指定格式,是指 Apache 所使用的正则表达式,通俗地将,是一种类似 * 的一种比较高级通配符。这种方法不需要下在额外的文件,只需要配置 Apache。
    方法三,mod_proxy_ajp,仅在 Apache 2.1 及以后的版本中可用,Apache 自带的一个新功能模块。这时 Apache 使用 Apache JServ Protocol 与 Tomcat 通讯。不需要下在额外的文件,需要改动Apache 和 Tomcat 的默认配置文件都需要改动。
    方法四,mod_proxy。其实 mod_proxy 既可以做类似于 Wingate 一样的公司局域网共享上网代理,也可以做反向代理(Reverse proxy)。这里使用的是反向代理功能,用户端浏览器不需要把代理服务器改成这里的 Apache 地址。mod_proxy 是 Apache 自带功能,并且配置比较简单。
     

    yum install gcc -y

    wget http://mirror.bjtu.edu.cn/apache//httpd/httpd-2.2.17.tar.gz

    wget http://www.oracle.com/technetwork/indexes/downloads/index.html

    wget http://labs.renren.com/apache-mirror/tomcat/tomcat-7/v7.0.8/bin/apache-tomcat-7.0.8.tar.gz

    我是用一台服务器 做的 环境如下

    Centos 5.5 64位 16核cpu  32G 内存

    Apache + tomcat (开启四个端口)

     

    #ar zxvf http-2.2.17.tar.gz
    #d httpd-2.2.17
    #/configure --prefix=/usr/local/apache --enable-modules=so --enable-mods-shared=all --enable-proxy --enable-proxy-connect --enable-proxy-ftp --enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer --enable-rewrite
    #make ; make install
    #cp support/apachectl /etc/init.d/httpd
    #chmod 755 /etc/init.d/httpd
    #vim /etc/init.d/httpd
    #chconfig –add httpd
    #chkconfig –level 345 httpd on
    cd ..
    chmod +x jdk-x64.bin          #添加执行权限
    ./jdk-x64.bin             #安装jdk
    mv jdk1.6.0_23/ /usr/java      
    vim /etc/profile    # 添加java环境变量
    #Java Environment
    JAVA_HOME=/usr/java
    JAVA_BIN=/usr/java/bin
    PATH=$PATH:$JAVA_HOME/bin
    CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    export JAVA_HOME JAVA_BIN PATH CLASSPATH
    source /etc/profile  
    #java –version
        java version "1.6.0_23"
        Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
        Java HotSpot(TM) 64-Bit Server VM (build 19.0-b09, mixed mode)
     
    #tar zxvf apache-tomcat-7.0.8.tar.gz
    #mv apache-tomcat-7.0.8 /usr/local/tomcat73
    #vim /etc/profile      # 添加tomcat 环境变量
    TOMCAT_HOME=/usr/local/tomcat73
    CATALINA_HOME=/usr/local/tomcat73
    CATLINA_BASE=/usr/local/tomcat73
    /usr/local/tomcat73/bin/catalina.sh start  #添加启动脚本
    export TOMCAT_HOME  CATALINA_HOME  CATALINA_BASH TOMCAT_BIN
    #source /etc/profile

     

     

    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_connect_module modules/mod_proxy_connect.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
    LoadModule rewrite_module modules/mod_rewrite.so
       把apache 虚拟主机前边的# 去掉
               Include conf/extra/httpd-vhosts.conf
       添加 Include conf/extra/httpd-vhosts.conf  如下
    <VirtualHost *80>
         ServerAdmin crazylinux@163.com
         ProxyRequests Off
         Proxypass / balancer://proxy/
         <Proxy balancer://proxy>
             Order Deny,Allow
             Allow from all
             BalancerMember http://127.0.0.1:8844         #tomcat  端口 (要用的端口,预设值的  可以随便设置)
             BalancerMember http://127.0.0.1:8855         #tomcat  端口
             BalancerMember http://127.0.0.1:8866          #tomcat  端口
             BalancerMember http://127.0.0.1:8877          #tomcat  端口
         </Proxy>
    </VirtualHost>
    保存修改  启动apache 服务
    #/usr/local/apache/bin/apachectl -k start
      
    修改 tomcat ,添加虚拟主机 按端口走 8844 8855 8866 8877 按照预设值的
    #vim /usr/local/tomcat73/conf/server.xml 只修改红色字体
      <Service name="Catalina0">
    <Connector port="8044" protocol="AJP/1.3" redirectPort="8443" />
        <Connector port="8844" />
        <Engine name="Catalina0" defaultHost="localhost" jvmRoute="jvm1">
          <Logger className="org.apache.catalina.logger.FileLogger" />
          <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                 resourceName="UserDatabase" />
          <Host name="localhost" appBase="webapps0" />
        </Engine>
      </Service>
      <Service name="Catalina1">
    <Connector port="8055" protocol="AJP/1.3" redirectPort="8443" />
        <Connector port="8855" />
        <Engine name="Catalina1" defaultHost="localhost" jvmRoute="jvm2">
          <Logger className="org.apache.catalina.logger.FileLogger" />
          <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                 resourceName="UserDatabase" />
          <Host name="localhost" appBase="webapps1" />
        </Engine>
      </Service>
      <Service name="Catalina2">
    <Connector port="8066" protocol="AJP/1.3" redirectPort="8443" />
        <Connector port="8866" />
        <Engine name="Catalina2" defaultHost="localhost" jvmRoute="jvm3">
          <Logger className="org.apache.catalina.logger.FileLogger" />
          <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                 resourceName="UserDatabase" />
          <Host name="localhost" appBase="webapps2" />
        </Engine>
      </Service>
      <Service name="Catalina3">
    <Connector port="8077" protocol="AJP/1.3" redirectPort="8443" />
        <Connector port="8877" />
        <Engine name="Catalina3" defaultHost="localhost" jvmRoute="jvm4">
          <Logger className="org.apache.catalina.logger.FileLogger" />
          <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                 resourceName="UserDatabase" />
          <Host name="localhost" appBase="webapps3" />
        </Engine>
      </Service>
    然后 创建 tomcat web 目录在/usr/local/tomcat73/   webapps0 webapps1 webapps2 webapps3 并在每个目录里面添加 index1.jsp 并添加 每个端口的对应号
    启动tomcat
    #/usr/local/tomcat73/bin/start.sh
    #/usr/local/apache/bin/apachetl –k restart
      查看端口  netsant –ntlp
    [root@localhost tomcat73]# netstat -ntlp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address            State       PID/Program name  
    tcp        0      0 0.0.0.0:964                 0.0.0.0:*                  LISTEN      2902/rpc.statd     
    tcp        0      0 0.0.0.0:111                 0.0.0.0:*                  LISTEN      2849/portmap       
    tcp        0      0 127.0.0.1:631               0.0.0.0:*                  LISTEN      3155/cupsd         
    tcp        0      0 127.0.0.1:25                0.0.0.0:*                  LISTEN      3178/sendmail: acce
    tcp        0      0 127.0.0.1:6010              0.0.0.0:*                  LISTEN      11977/1            
    tcp        0      0 :::8866                     :::*                       LISTEN      15836/java         
    tcp        0      0 :::8066                     :::*                       LISTEN      15836/java         
    tcp        0      0 ::ffff:127.0.0.1:8005       :::*                       LISTEN      15836/java         
    tcp        0      0 :::8009                     :::*                       LISTEN      15836/java         
    tcp        0      0 :::8844                     :::*                       LISTEN      15836/java         
    tcp        0      0 :::8044                     :::*                       LISTEN      15836/java         
    tcp        0      0 :::8877                     :::*                       LISTEN      15836/java         
    tcp        0      0 :::8077                     :::*                       LISTEN      15836/java          
    tcp        0      0 :::80                       :::*                       LISTEN      15987/httpd        
    tcp        0      0 :::8080                     :::*                       LISTEN      15836/java         
    tcp        0      0 :::22                       :::*                       LISTEN      25432/sshd         
    tcp        0      0 :::8855                     :::*                       LISTEN      15836/java         
    tcp        0      0 :::8055                     :::*                       LISTEN      15836/java         
    tcp        0      0 ::1:6010                    :::*                       LISTEN      11977/1 
     
     
    看到了 80  8844 8855 8866 8877  那tomcat 虚拟主机  和 apache都启动了 访问下试试吧
    推荐内容