nginx+keepalived+tomcat之具体配置档

前沿知识点:

  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为备sessionRequest请求到来时, 判断tomcat容器是否发生变化,若变化(即原tomcat down掉),则memcached加载备sessiontomcat2,响应给客户端,请求结束后,重置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 为备sessionRequest请求到来时,从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>

...

}

 



附件列表

 

转载于:https://www.cnblogs.com/aaa103439/p/3588673.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值