Nginx+Tomcat出现session丢失问题

Nginx做为一个强大的Web服务器,有很强的功能。在Nginx+Tomcat是Java Web动静分离的很好模型。可是,今天在配置过程当中,遇到了一个问题,就是没法登陆。因为以前在配置Apache的过程当中,遇到过相似的问题,因此我很快肯定了是因为sessionId在cookies中引发。 web

那么,咱们如何在Nginx中配置,以免这样的事情呢?我是经过以下代码来解决的。 shell

场景描述一下: 服务器

我有一个二级域名mvn.domain.com,以及一个web程序,部署到个人服务器中,访问地址为:http://localhost:8081/nexus,经过个人域名,我但愿反向代理到个人本地地址中。因为,我在部署本地应用的时候,不是连接到根目录(即root),致使个人应用程序的sessionId存储在/nexus的path下。而当我成功后,mvn.domain.com中,找不到对应的sessionId信息,致使会话失效。 cookie

解决方法一: session

域名使用mvn.domain.com/nexus,反向代理到http://localhost:8081/nexus,能够解决如上问题。配置以下: dom

location /nexus/ {
                proxy_pass    http://localhost:8081/nexus/;
                proxy_redirect  off;
                proxy_set_header        Host    $http_host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   Cookie $http_cookie;
                chunked_transfer_encoding       off;
        }
        location / {
                proxy_pass    http://localhost:8081/nexus/;
                proxy_redirect  http://localhost:8081/ http://mvn.domain.com/;
        }



解决方法二,

        location / {
                proxy_pass    http://localhost:8081/nexus/;
                proxy_redirect off;
                proxy_set_header        Host    $http_host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   Cookie $http_cookie;                 
                rewrite ^/nexus/(.*)$ /$1 last;
                proxy_cookie_path /nexus /;
                chunked_transfer_encoding       off;
        }

说明:
1. 经过proxy_pass 来肯定对应目录的跳转
2. 在多层代理中,填充Header请求头(proxy_set_header)
3. 将应用中访问的nexus路径rewrite到根路径下。
4. 将cookie_path为/nexus设置到根路径下。
spa

相关文章
相关标签/搜索