nginx+keepalived+tomcat之具体配置档

系统 2121 0

前沿知识点:

  1. nginx负责负载均衡(反向代理)

  2. msm(memcached session manager)负责缓存会话信息,从而实现会话保持

所需包:

  1. nginx和memcached采用最新稳定版

  2. tomcat版本需要与msm版本一致,这里采用tomcat7

  3. msm包共计9个包,包名具体信息查看附件,msm的所有包放到$CATALINA_HOME/lib

配置过程:

nginx的配置信息如下-->


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
...
 
http {
 
     ...
 
     upstream tomcat {
 
         server node1:8080;
 
         server node2:8080;
 
#这里具体使用什么算法,暂定,不过我觉得ip_hash不好,会造成负载不均衡
 
     }
 
     server {
 
     ...
 
         location ~* ^ /testapp {
 
             proxy_pass http: //tomcat ;
 
             proxy_redirect off;
 
             proxy_set_header X-real-ip $remote_addr;
 
             proxy_set_header X-forwarded- for $proxy_add_x_forwarded_for;
 
             proxy_set_header Host $host;
 
         }
 
     ...
 
     }
 
}

 


tomcat的配置信息如下-->


首先修改server.xml,在默认的host flag中添加context

1
2
3
4
5
6
7
8
9
10
11
12
13
...
 
     < host >
 
     ...
 
     < context path = "/testapp" docbase = "testapp/" />
 
     ...
 
     </ host >
 
...

 

其次修改context.xml,在默认的context flag中添加manager

其中粘性session方式如下:  Sticky  模式 tomcat 本地容器 session  session  memcached 为备 session Request 请求到来时,  判断tomcat容器是否发生变化,若变化(即原tomcat down掉),则 memcached 加载备 session tomcat2 ,响应给客户端,请求结束后,重置session_id,并更新到memcached  

 

  
1
2
3
4
5
6
7
8
9
< Context >
   ...
   < Manager className = "de.javakaffee.web.msm.MemcachedBackupSessionManager"
     memcachedNodes = "n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
    failoverNodes = "n1"
     requestUriIgnorePattern = ".*\.(ico|png|gif|jpg|css|js)$"
     transcoderFactoryClass = "de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
     />
</ Context >
  

 

非粘性session如下:  Non-Sticky 模式 tomcat session    中转 session  memcached1  为主, memcached 2  为备 session Request 请求到来时,从 memcached 2 加载备  session   tomcat ,(另外,若 只有一个 memcached 节点,或者 memcached2  出错时,且 tomcat本地 容器 中还没有 session, 则从 memcached1 加载主  session   tomcat ), Request 请求结束时,将 tomcat session 更新至   memcached1 和备 memcached2 ,并且清除 tomcat session  。以达到主备同步之目的,如下是non-sticky模式的响应流程图:(图片来源网络)。

此模式下,基于session的验证码将无法使用,因为此模式下,第一次session是用本地,然后存放到mem1和mem2中,之后客户再次请求,则路由到了另外一台tomcat上,又因为是同一个session_id,所以使用的是mem1中,但是mem1中确是旧的session.(但是验证码要求每一次都不一样...)

 

 

 

  
1
2
3
4
5
6
7
8
9
10
11
< Context >
   ...
  < ManagerclassName = "de.javakaffee.web.msm.MemcachedBackupSessionManager"
    memcachedNodes = "n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
    sticky = "false"
    sessionBackupAsync = "false"
    lockingMode = "uriPattern:/path1|/path2"
    requestUriIgnorePattern = ".*\.(ico|png|gif|jpg|css|js)$"
    transcoderFactoryClass = "de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
    />
</ Context >
  

 


keepalived配置信息如下-->这里只贴出主的,从的就不贴了

 


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
! Configuration File for keepalived
 
global_defs {
    notification_email {
      acassen@firewall.loc
      failover@firewall.loc
      sysadmin@firewall.loc
    }
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}
#<Spinestars
vrrp_script chk_keepalived_down {
     script "[ -f /var/run/keepaliveddown ] && exit 1 || exit 0"
     interval 2
     weight -2
}
#nginx_check_script
vrrp_script chk_nginx {
     script "killall -0 nginx && exit 0 || exit 1"
     interval 2
     weight -2
}
#/Spinestars>
vrrp_instance VI_1 {
     state MASTER
     interface eth1
     virtual_router_id 20
     mcast_src_ip 192.168.100.1
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {
         10.88.100.2
     }
     track_script {
     chk_nginx
     chk_keepalived_down
     }
}

 

nginx动静分离配置:

 

...

#<Spinestars
upstream tomcat_servers {
        server node1 : 8080 ;
        server node2 : 8080 ;
}
server {
        listen       * : 80 ;
        server_name   test .shop.com;
        root / var / www / shop;
        index index.html index.jsp index.htm;
#/Spinestars> 


#<Spinestars

        location  ~ *  \.(html | jpg | png | jpeg | css | gif | ico)$ {

           root  / var / www;

        }

        location  ~ *  \.(js | jhtml)$ {

                proxy_pass http : / / tomcat_servers;

                proxy_redirect off;

                proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;

                proxy_set_header Host $host;

                proxy_set_header X - Real - IP $remote_addr;

        }

         if  ( $host  =   'test.shop.com'  ){

                rewrite  ^ / http : / / test .shop.com / shop  permanent;

        }
         #以下location是测试用的
        location  ~ *   ^ / testapp {

                proxy_pass http : / / tomcat_servers;

                proxy_redirect off;

                proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;

                proxy_set_header Host $host;

                proxy_set_header X - Real - IP $remote_addr;

        }

#/Spinestars>

...

}

 



 

nginx+keepalived+tomcat之具体配置档


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论