【发布时间】:2014-12-17 23:08:37
【问题描述】:
我目前正在使用 Spring 和 Shiro 开发应用程序。我正在部署到 Tomcat 7,在生产中我使用 nginx 作为反向代理。除了jsessionid 在通过 nginx 代理访问应用程序时添加到每个 URL 之外,一切都运行顺利(很好)。
当我使用以下 nginx 配置时:
server {
server_name example.com www.example.com;
listen 80;
location /myapp {
proxy_pass http://localhost:8080;
}
}
我通过 www.example.com/myapp 访问该应用程序,然后一切正常 - URL 中没有 jsessionid
当我使用以下配置时:
server {
server_name sub.example.com www.sub.example.com
listen 80;
location / {
proxy_pass http://localhost:8080/myapp/;
}
我通过 www.sub.example.com 访问该应用程序,然后我看到每个 URL 都添加了jsessionid(即使在成功登录后也是如此)。
我发现了类似的帖子,建议在 web.xml 中添加以下内容:
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
这行得通 - 好吧,jsessionid 已删除但我无法进行身份验证,这让我认为 nginx 中存在 cookie 配置问题,有什么建议吗?
EDIT//:找到了解决办法,只需要在nginx config中添加以下内容:
proxy_cookie_path /myapp/ /;
【问题讨论】:
-
您确认这确实是 cookie 问题吗?在生产中直接访问tomcat时可以进行身份验证吗?还要检查网络浏览器中 cookie 的值(使用 chrome 控制台或其他东西),它是否被设置并且值不会改变?
-
直接访问tomcat可以认证没问题。即使使用第一个 nginx 配置访问也可以。
标签: spring nginx proxy shiro jsessionid