首页 > 代码库 > LAMT&LNMT

LAMT&LNMT

Tomcat运行方式

1.standalone configure:tomcat作为独立的web服务器和jsp服务器

    request ----> web server(tomcat) ----> servlet container

66513265

2.进程间配置,在一台主机上,web由Apache或nginx提供,jsp服务有servlet提供

image

3.独立网络配置,把提供web服务的主机独立出来,一台主机提供web服务,一台主机提供jsp服务

image

web/Apache和后端的servlet通信有两种方式:

1.mod_jk :Apache/1.3, Apache/2.0;mod_jk常用的版本是jk,jk2.0反倒不建议使用,Apache 2.0以前不支持使用mod_proxy

2.mod_proxy :Apache/2.2+ , Apache 2.2以后要使用mod_jk需要编译安装

注:通信协议有 http(https)和ajp

mod_jk V.S. mod_proxy:
        负载均衡 : mod_proxy稍弱一点
        管理接口 : 都支持基于web页面的管理接口
        兼容性:
        配置 : mod_proxy较简单
        协议:mod_jk (ajp)通常不使用http, mod_proxy(http/https/ajp)

web/nginx和后端的servlet通信只能使用http协议

LAMT:
        apache(mod_jk, ajp) + tomcat(ajp )
        apache(mod_proxy, (http, https, ajp)) + tomcat(http, https, ajp)
LNMT:
        nginx + tomcat(http, https)


LNMT实现

48993359

安装nginx:

  1: rpm -ivh nginx-1.4.7-1.e16.ngx.x86_64.rpm
  2: vim /etc/nginx/nginx.conf
  3: location / {
  4:     #         root   html;
  5:     #         index  index.html index.htm;
  6:          proxy_pass http://192.168.21.1;  #只需加入这一句即可实现最简单的代理功能,不过我们这里不这样做
  7:          }

配置nginx实现反向代理tomcat和动静分离

  1: upstream tcser {   #添加后端tomcat主机,这是定义在http段的
  2:         server 192.168.21.1:8080;
  3:         server 192.168.21.2:8080;
  4:         }
  5: location / { 这是server段的
  6:             root   html;
  7:             index  index.jsp index.html index.htm;  增加一个index.jsp的默认主页
  8:         }   
  9:  location ~* \.(jsp|do)$ {    #所有jsp和do结尾的页面都代理至后端服务器器
 10:         proxy_pass http://tcser;
 11:         }   


74366218

Apache反代-->mod_proxy,将所有网页内同都代理至后端,http协议

  1: vim /etc/httpd/conf.d/mod_proxy.conf
  2: ProxyVia on
  3: ProxyRequests off 
  4: ProxyPreserveHost on
  5: 
  6: ProxyPass / http://192.168.21.1/
  7: ProxyPassReverse / http://192.168.21.1/
  8: 
  9: <Location />
 10: Order Allow,Deny
 11: Allow from all
 12: </Location>

Apache反代-->mod_proxy,将所有网页内同都代理至后端,ajp协议

  1: ProxyVia on
  2: ProxyRequests off
  3: ProxyPreserveHost on
  4: 
  5: ProxyPass / ajp://192.168.21.1:8009/  #ajp监听的端口是8009
  6: ProxyPassReverse ajp/ http://192.168.21.1:8009/
  7: 
  8: <Location />
  9: Order Allow,Deny
 10: Allow from all
 11: </Location>

Apache反代实现负载均衡

前端Apache服务器:外网:172.16.21.1,内网:192.168.21.3

后端tomcat1:192.168.21.1,后端tomcat2:192.168.21.2

  1: vim /etc/httpd/conf.d/mod_proxy.conf
  2: ProxyVia on
  3: ProxyRequests off 
  4: ProxyPreserveHost on
  5: <Proxy balancer://lb>
  6: BalancerMember http://192.168.21.2 loadfactor=1
  7: BalancerMember http://192.168.21.1 loadfactor=1
  8: </Proxy>
  9: ProxyPass / balancer://lb/
 10: ProxyPassReverse / balancer://lb/
 11: <Location />
 12: Order Allow,Deny
 13: Allow from all
 14: </Location>

Apache反代实现负载均衡以及session绑定

  1: vim /etc/httpd/conf.d/mod_proxy.conf
  2: ProxyVia on
  3: ProxyRequests off 
  4: ProxyPreserveHost on
  5: <Proxy balancer://lb>
  6: BalancerMember http://192.168.21.2 loadfactor=1 route=TomcatB
  7: BalancerMember http://192.168.21.1 loadfactor=1 route=TomcatA
  8: </Proxy>
  9: ProxyPass / balancer://lb/ stickysession=JSESSIONID
 10: ProxyPassReverse / balancer://lb/
 11: <Location />
 12: Order Allow,Deny
 13: Allow from all
 14: </Location>
 15: --------后端服务器需添加----
 16: <Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA"> 注意与上面的IP地址对应即可
 17: <Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">

Apache反代实现负载均衡以及session绑定添加管理器

  1: vim /etc/httpd/conf.d/mod_proxy.conf
  2: ProxyVia on
  3: ProxyRequests off
  4: ProxyPreserveHost on
  5: <Proxy balancer://lb>
  6: BalancerMember http://192.168.21.2 loadfactor=1
  7: BalancerMember http://192.168.21.1 loadfactor=1
  8: </Proxy>
  9: <Location /lbmanager>  定义管理web功能
 10: SetHandler balancer-manager
 11: </Location>
 12: Proxypass /lbmanager !  #这里表示/lbmanager不向后转发
 13: ProxyPass / balancer://lb/
 14: ProxyPassReverse / balancer://lb/
 15: <Location />
 16: Order Allow,Deny
 17: Allow from all
 18: </Location>