Nginx+memcached+tomcat Session Sharing

Source: Internet
Author: User
Tags memcached nginx server tomcat server

Server Description:

192.168.1.8 TOMCAT1 memcached (virtual machine 1)

192.168.1.9 tomcat2 (Virtual machine 2)

192.168.1.200 nginx (native)

For Tomcat+nginx Configuration Please refer to the blog: "nginx-reverse Proxy Server" practice Chapter (iii) of the Tomcat+nginx build a cluster

Let's install memcached and pay attention to the Linux system User rights issue.

1. Download the memcached and libevent back to the/USR/SRC directory, respectively:

# CD/USR/SRC
# wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz
# wget http://www.monkey.org/~provos/libevent-1.2.tar.gz

2. Install Libevent First:

# tar ZXVF libevent-1.2.tar.gz
# CD libevent-1.2
#./CONFIGURE–PREFIX=/USR
# make
# make Install

3. Test whether the Libevent is installed successfully:

# Ls-al/usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 11?? 17:38 Libevent-1.2.so.1-libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 263546 11?? 17:38 libevent-1.2.so.1.0.3
-rw-r–r–1 root root 454156 11?? 17:38 LIBEVENT.A
-rwxr-xr-x 1 root root 811 11?? 17:38 libevent.la
lrwxrwxrwx 1 root root 21 11?? 17:38 libevent.so-libevent-1.2.so.1.0.3

4. Install the memcached and require the installation location of the specified libevent in the installation:

# cd/tmp
# tar ZXVF memcached-1.2.0.tar.gz
# CD memcached-1.2.0
#./CONFIGURE–WITH-LIBEVENT=/USR
# make
# make Install
If there is an error in the middle, please carefully check the errors, follow the error message to configure or add the appropriate library or path.
When the installation is complete, the memcached will be placed on the/usr/local/bin/memcached

5. Test whether the memcached is installed successfully:

# ls -al/usr/local/bin/mem*
-rwxr-xr-x 1 root root 137986 11?? 17:39/usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 17:39/usr/local/bin/memcached-debug

6. Start Memcache

Useradd memcache #添加memcache用户

Memcached-m 64-d-u memcache-p 11211-l 192.168.1.8-c 5000-p/tmp/memcached.pid

or memcached-m 64-d-u root-p 11211-l 192.168.1.8-c 5000-p/tmp/memcached.pid

Note: Start the Times again when a port is occupied, kill the memcache process, and then start two times before it succeeds.

Tomcat also requires additional configuration as follows:

Tomcat connection Memcache Session sharing configuration: Session stored in 192.168.1.8:11211 memcache inside.

Vi/usr/local/tomcat/conf/context.xml #将下面配置复制到 between <Context> and </Context>

Two hosts doing the same configuration

[HTML]View PlainCopyprint?
  1. <span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><Manager classname="De.javakaffee.web.msm.MemcachedBackupSessionManager"
  2. memcachednodes="node1:192.168.1.8:11211"
  3. requesturiignorepattern=". * *. ( PNG|GIF|JPG|CSS|JS) $ "
  4. sticky="false"
  5. storagekeyprefix="context"
  6. lockingmode="Uripattern:/path1|/path2"
  7. sessionbackupasync="false"
  8. sessionbackuptimeout="
  9. copycollectionsforserialization="false"
  10. transcoderfactoryclass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
  11. </span>

Description

The memcachednodes here is to fill the memcached node, where multiple nodes can be separated by an empty compartment, such as:
n1:localhost:11211 n2:localhost:11212/localhost instead install IP for memcached server
Sessionbackuptimeout units are in minutes

Of course, after the configuration is completed, you need to add the appropriate jar package, the following jar package is placed in two Tomcat Lib directory.

After modification, restart two Tomcat and Nginx, this time has solved the session sharing problem

Start Tomcat and print log to see the problem in a timely manner

Test Nginx and Tomcat

Test Nginx Whether the load balancer is able to share session information, the two Tomcat server in the \webapps\root directory to create a new two test.jsp page, the content is as follows:

Tomcat1

[HTML]View PlainCopyprint?
  1. <span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;"  ><%@ page contenttype="Text/html;charset=utf-8" iselignored="false"%>
  2. SessionID:<%=session.getid ()%>
  3. <BR>
  4. Sessionip:<%=request.getservername ()%>
  5. <BR>
  6. Sessionport:<%=request.getserverport ()%>
  7. <%
  8. Out.println ("This is Tomcat Server 1111111");
  9. %>
  10. </span>

Tomcat2

[HTML]View PlainCopyprint?
  1. <span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;"  ><%@ page contenttype="Text/html;charset=utf-8" iselignored="false"%>
  2. SessionID:<%=session.getid ()%>
  3. <BR>
  4. Sessionip:<%=request.getservername ()%>
  5. <BR>
  6. Sessionport:<%=request.getserverport ()%>
  7. <%
  8. Out.println ("This is Tomcat Server 2222222");
  9. %></span>



Access the Nginx server through a browser, such as changing the access address to the server where Nginx resides.

Refresh the browser repeatedly, if the SessionID has been unchanged, the following sessionport content is changing, indicating that the configuration is successful.

The test results are as follows:

Refresh Browser

And then open Firefox to see the results.

Refresh Firefox browser

Report:

1, when the session is not shared, the two Tomcat servers have a weight ratio of 1:1

Effect: Refresh the browser, each time the SessionID are different

Cause: When the browser is first accessed, the session is created, then the SessionID is stored in the browser's cookie, and when the browser refreshes or accesses another page, the local SessionID, but the request is distributed to another server. The past SessionID on the new server and could not find the corresponding session, so the session is re-generated. So every refresh is different sessionid,session cannot be shared.

2, when the session is shared, the two Tomcat servers have a weight ratio of 1:1

Effect: Refreshes the browser, each time the SessionID is not changed, but the printed this is the Tomcat server value will change

Cause: When the browser first accesses, creates the session, and saves the SessionID in the browser, then when the user refreshes the browser or accesses the other page, our client request will take the SessionID, then we through the SessionID to find the session, At this point, regardless of the request distribution to which server above, we can find the session, because we stored the session to the Memcached server, so the session will not be recreated, and the session is shared.

Download the required JAR package

Nginx+memcached+tomcat Session Sharing

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.