---
title: "apache集群，多web应用，粘性会话 不起作用问题的简单解决方法"
author: "nihongye"
date: "2010-11-30T23:12:57Z"
tags: ["Apache", "C", "C#", "C++", "Web"]
categories: ["编程语言"]
slug: "apache-web"
draft: false
lastmod: "2010-11-30T23:24:36Z"
id: 829821
url: "https://nihongye.iteye.com/blog/829821"
---

今天系统部署，发现web应用之间，粘性会话的配置不起作用。因为配置stickysession=JSESSIONID而JSESSIONID生成在各个应用的path下。
在apache中的mod_proxy_balancer.c中:

```java
static char *get_cookie_param(request_rec *r, const char *name)
{
    const char *cookies;
    const char *start_cookie;

    if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
        for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
             start_cookie = ap_strstr_c(start_cookie + 1, name)) {
            if (start_cookie == cookies ||
                start_cookie[-1] == ';' ||
                start_cookie[-1] == ',' ||
                isspace(start_cookie[-1])) {

                start_cookie += strlen(name);
                while(*start_cookie && isspace(*start_cookie))
                    ++start_cookie;
                if (*start_cookie == '=' && start_cookie[1]) {
                    /*
                     * Session cookie was found, get it's value
                     */
                    char *end_cookie, *cookie;
                    ++start_cookie;
                    cookie = apr_pstrdup(r->pool, start_cookie);
                    if ((end_cookie = strchr(cookie, ';')) != NULL)
                        *end_cookie = '\0';
                    if((end_cookie = strchr(cookie, ',')) != NULL)
                        *end_cookie = '\0';
                    return cookie;
                }
            }
        }
    }
    return NULL;
}
```

apache没由提供cookie path的设置，因此按照cookie的优先顺序，将首先取到与应用上下文中接近的jsessionid，所以显然，如果各个应用都有同名的sticksession，其值又不同，那么apache粘性会话不能跨web应用。

解决方法也显然有二:
*一.修改mod_proxy代码，提供在指定的cookie path读取sticksession的配置
(发现在服务端读不了cookie path，所以此路不同，哈哈)*
二.不使用jsessionid作为stickysession的名字,自己定义一个，并在/添加cookie,如myjsessionid=${your uuid}.server1，其中server1为route id。