The session problem of Nginx+tomcat load balancing

Source: Internet
Author: User

Nginx and Tomcat do load balancing when there is an important issue to consider, that is the session.

Imagine that, assuming Nginx has a polling policy, the application is deployed to TOMCAT1 and TOMCAT2, and the user is asked to log on when they access the TOMCAT1, so the user logs on. The next time the user launches the application request, will be processed by TOCMAT2 (because polling, the request is alternately forwarded to TOMCAT1 and TOMCAT2), this time tomcat2 found that the user is not logged in, and then asked the user to log in, this is the user is very puzzled ah, I just login success Ah, How to log in again. The reason for this is that the user session is saved on TOMCAT1, and when he accesses TOMCAT2, the user does not actually log on to TOMCAT2.

Accreditations, what is the proof? Here's an example to verify what's said above:

The example is this: the user accesses the app test, enters the user name and password after login, fills in his favorite color and the fruit, then submits, the page displays the user to fill in the result. If the user is not logged in, the appropriate information is output from the console.


The project structure is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/65/wKioL1WbZOfwbmasAADNLLm-GtU701.jpg "style=" float: none; "title=" 8_ project structure. png "alt=" wkiol1wbzofwbmasaadnllm-gtu701.jpg "/>

The filters are configured as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/68/wKiom1WbYxnDuVw7AAN1a-YbnWk335.jpg "style=" float: none; "title=" 9_sessionfilter.png "alt=" Wkiom1wbyxnduvw7aan1a-ybnwk335.jpg "/>

The Web. XML is configured as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/68/wKiom1WbY67iqcUWAAWC6_066nc375.jpg "title=" 2015-07-07_132844.png "alt=" Wkiom1wby67iqcuwaawc6_066nc375.jpg "/>
The Nginx configuration is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/65/wKioL1WbZfKSGgtvAAGoDv9OOt4146.jpg "title=" 7_ Nginx configuration file. png "alt=" wkiol1wbzfksggtvaagodv9oot4146.jpg "/>

The user operates as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/65/wKioL1WbZk6Bto7PAAPdLsUdUaQ782.jpg "title=" 11_ Page change process. png "alt=" wkiol1wbzk6bto7paapdlsuduaq782.jpg "/>

The server processing process is analyzed as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6F/68/wKiom1WbZLLg_YmuAAICqqztipw564.jpg "title=" 10_ Processing process. png "alt=" wkiom1wbzllg_ymuaaicqqztipw564.jpg "/>

Finally, the two Tomcat console outputs are as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6F/68/wKiom1WbZPqRiIiSAAQmNNHvgcs717.jpg "style=" float: none; "title=" 5_TOMCAT1 console. png "alt=" wkiom1wbzpqriiisaaqmnnhvgcs717.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/66/wKioL1WbZsjRxvp4AAQMX3DtMqg761.jpg "style=" float: none; "title=" 6_TOMCAT2 console. png "alt=" wkiol1wbzsjrxvp4aaqmx3dtmqg761.jpg "/>

From the console output, we can see: intercepted and redirected to the home page when accessing TOMCAT1


This article is from the "ten-li Rice flower" blog, please be sure to keep this source http://5880861.blog.51cto.com/5870861/1671636

The session problem of Nginx+tomcat load balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.