温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux下实现apache代理tomcat

发布时间:2020-03-02 15:42:26 来源:网络 阅读:530 作者:Insane_linux 栏目:建站服务器

apache+tomcat
Linux下实现apache代理tomcat
http版本2.4
1.http+tomcat通信了解:
   http和tomcat通信通过ajp,http,https协议进行通信
   http与tomcat基于mod_jd和mod_proxy实现负载均衡
1.安装http

a.下载:
apache http:http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.25.tar.gz
arp:http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.gz
arp-util:http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
b.编译安装
1.先安装pcre-devel
    yum install pcre-devel2.先安装arp
    >tar -xzf apr-1.5.2.tar.gz
    >cd apr-1.5.2
    >./configure  --prefix=/usr/local/apr
    >make && make install3.安装arp-util
    >tar -xzf apr-util-1.5.4.tar.gz
    >cd apr-util-1.5.4
    >./configure --prefix=/usr/local/apt-util --with-apr=/usr/local/apr
    >make && make insatll4.安装httpd
    >tar -xzf httpd-2.4.25.tar.gz
    >cd httpd-2.4.
    >./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable--ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modeles=most --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-http --enable-proxy-ajp --enable-balencer
    >make && make install5.配置httpd的环境
    (a).配置httpd服务启动文件
    vim /etc/init.d/http
    . /etc/rc.d/init.d/functions    if [ -f /etc/sysconfig/httpd ]; then
             . /etc/sysconfig/httpd    fi
    HTTPD_LANG=${HTTPD_LANG-"C"}
    INITLOG_ARGS=""
    apachectl=/usr/sbin/httpd/bin/apachectl
    httpd=/usr/local/httpd/bin/httpd}
    prog=httpd
    pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
    lockfile=${LOCKFILE-/var/lock/subsys/httpd}
    RETVAL=0
    STOP_TIMEOUT=${STOP_TIMEOUT-10}
    start() {          echo -n $"Starting $prog: "
          LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
         RETVAL=$?         echo
         [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
    }    stop() {
          status -p ${pidfile} $httpd > /dev/null          if [[ $? = 0 ]]; then
              echo -n $"Stopping $prog: "
              killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
          else
              echo -n $"Stopping $prog: "
              success          fi
          RETVAL=$?          echo
          [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
    }    reload() {     echo -n $"Reloading $prog: "
      if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
          RETVAL=6
          echo $"not reloading due to configuration syntax error"
          failure $"not reloading $httpd due to configuration syntax error"
      else
          # Force LSB behaviour from killproc
          LSB=1 killproc -p ${pidfile} $httpd -HUP
          RETVAL=$?          if [ $RETVAL -eq 7 ]; then
              failure $"httpd shutdown"
          fi
      fi
      echo
  }   case "$1" in
    start)
         start;;
    stop)         
     stop;;
    status)
         status -p ${pidfile} $httpd
         RETVAL=$?;;
   restart)
         stop
         start;;
   condrestart|try-restart)         if status -p ${pidfile} $httpd >&/dev/null; then
            stop
            start         fi;;
   force-reload|reload)
         reload;;
   graceful|help|configtest|fullstatus)         $apachectl $@
         RETVAL=$?;;
   *)         echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|forc    e-reload|reload|status|fullstatus|graceful|help|configtest}"
          RETVAL=2
 esac
 exit $RETVAL

     (b).导出头文件
    ln -sv /usr/local/httpd/include /usr/include/httpd
    (c).导出帮助文件
        vim /etc/man.config
        MANPATH /usr/local/htppd/man
    (d).修改环境变量
        vim /etc/profile.d/httpd        export PATH=/usr/local/httpd/bin:$PATH        123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107

2.基于mod_proxy实现httd+tomcat
   a.基于ajp协议

ProxyVia off        ProxyRequests offProxyPreserveHost off<Proxy *>
    Require all granted</Proxy>ProxyPass / ajp://192.168.100.34:8009/ProxyPa***everse / ajp://192.168.100.34:8009/<Location />
    Require all granted</Location>12345678910111234567891011

b.基于http协议

ProxyVia OffProxyRequests OffProxyPass / http://192.168.100.34:8080/ProxyPa***everse / http://192.168.100.34:8080/<Proxy *>
    Require all granted</Proxy><Location />Require all granted</Location>1234567891012345678910

c.参数详解

ProxyVia {On|Off|Full|Block}:用于控制http首部是否时哟您Via,主要用于多级代理中控制代理请求的流向,Full表示每个请求报文都会添加apache服务器的版本号信息,Block表示每个代理报文中Via都将被清除。
ProxyPreserveHost {On|Off}:启用此功能,代理会将用户请求报文中的Host行发送给后端服务器,而不使用Proxy_pass指定的服务器地址,如果在反向代理中支持虚拟主机,则需要开启此项。
ProxyRequests {On|Off}:是否开启正向代理,如果设置proxy_pass这个必须设置为OffProxyPass path url [key=value key=value..];将后端服务器url和本地某虚拟路径关联起来作为提供服务的;路径,path是当前服务器上的某虚拟路径,如果path以/结尾,这url也必须以/结尾。
    常用key如下:
    min:连接池的最小容量,初始化时的容量。
    max:连接池的最大容量,
    loadfactor:用于负载均衡集群配置中,定义对应后端服务器的权重
    retry:apache在后端服务器得到错误响应时,多长时间之后在重试,单位为秒,若一致没有响应,则会移除,重新上线以后会添加进来
ProxyPa***everse:用于让apache调整HTTP重定向响应报文中的Location,Context-Location及URL标签所对应的URL,在反向代理中必须使用此指令,防止重定向报文绕过proxy服务器1234567891012345678910

d.测试
Linux下实现apache代理tomcat
Linux下实现apache代理tomcat
3.基于mod_jk实现http+tomcat
   a.编译安装mod_jk

1.下载:
http://mirror.bit.edu.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.42-src.tar.gz2.安装:    >tar -xzf tomcat-connectors-1.2.42-src.tar.gz    >cd tomcat-connectors-/native/
    >./configure --with-apxs=/usr/local/http/bin/apxs(如果http是yum安装,请安装httpd-devel,rpm -ql httpd-devel找到apxs路径)    >make && make install12345671234567

b.配置/etc/httpd.d/httpd.conf

1.vim /etc/httpd.d/httpd.conf
LoadModule jk_module modules/mod_jk.so  //装载模块JkWorkersFile /etc/httpd/extra/workers.properties   //设置workers文件JkLogFile logs/mod_jk.log   //定义日志文件JkLogLevel debug        //日志级别JkMount /* TomcatA      //根下面的所有路径都转发给tomcatAJkMount /status/ stat1      //查看状态页面2.vim /etc/httpd/extra/workers.properties
worker.list=TomcatA,stat1
worker.TomcatA.port=8009worker.TomcatA.host=192.168.100.34worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1worker.stat1.type=status        //状态页面12345678910111213141234567891011121314

c.workers.properties详解:

格式
worker.list = worker.name worker.nameworker.worer_name.property =valueproperty:
    host:tomcat实例所在主机
    port:所在端口
    connection_pool_minsize:最少保持在连接池中链接的个数
    connection_pool_timeout:连接池中链接超时时长
    retries:错误发生时重试次数
    socket_timeout:mod_jk等待worker响应时长,默认伪0,无限等待
    socket_keepalive:是否启动长链接,1表示启用
    lbfactor:worker的权重
    type:指定类型,值伪status表示状态信息,ajp13表示当前worker伪运行的一个实例,lb表示使用负载均衡1234567891011121312345678910111213

d.测试结果
Linux下实现apache代理tomcat

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI