Nginx + Tomcat + Memcached cluster must take into account at least 2 aspects of the problem:
1. How to achieve load balancing?
2. How to achieve session replication and synchronization?
This article collects the related material on the net, the construction process of Nginx + Tomcat + Memcached cluster under window is summarized as follows.
First, the development environment
1. Nginx Version: 1.8.0
2. Tomcat version: 7.0.55
3. memcached Version: 1.4.13
4. Session replication synchronization using Memcache-session-manager Latest version: 1.6.3
5. Operating system: Win7
Second, Nginx + Tomcat to achieve load balancing
1. Installation of nginx and tomcat download
There is not much to say, see the download installation of Nginx and Tomcat specifically
2. Nginx Configuration
Open Nginx Main profile: D:\nginx-1.8.0\conf\nginx.conf, slightly modified, the modified file as follows (red Bold section for my configured proxy forwarding server IP and port):
#user nobody;worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;events {worker_connections 1024;} HTTP {include mime.types; Default_type Application/octet-stream; #log_format Main ' $remote _addr-$remote _user [$time _local] "$request" ' # ' $status $body _bytes_sent "$http _referer" ' # ' "$http _user_agent" "$http _x_forwarded_for"; #access_log Logs/access.log Main; Sendfile on; #tcp_nopush on; #keepalive_timeout 0; Keepalive_timeout 65; #gzip on;<span style= "color: #FF0000;" ><strong>upstream local_tomcat {server localhost:18080 weight=1; Server localhost:18081 weight=1; } </strong></span> server {listen 80; #charset Koi8-r; #access_log Logs/host.access.log Main; Location/{root HTML; Index Index. html index.htm; <span style= "color: #FF0000;" ><strong>proxy_pass Http://local_tomcat; </strong></span>} #error_page 404/404.html; # REDIRECT Server error pages to the static page/50x.html # Error_page 502 503 504/50x.html; Location =/50x.html {root html; } # Proxy The PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ {# ProX Y_pass http://127.0.0.1; #} # Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root HTML; # Fastcgi_pass 127.0.0.1:9000; # Fastcgi_index index.php; # Fastcgi_param Script_filename/scripts$fastcgi_script_name; # include Fastcgi_params; #} # Deny access to. htaccess files, if Apache ' s document Root # concurs with Nginx ' s one # #location ~/\.ht {# deny all; #}} # Another virtual host using mix of ip-, name-, and port-based configuration # #server {# listen 8000; # Listen somename:8080; # server_name somename alias Another.alias; # location/{# root HTML; # index index.html index.htm; #} #} # HTTPS Server # #server {# listen 443 SSL; # server_name localhost; # ssl_certificate Cert.pem; # Ssl_certificate_key Cert.key; # Ssl_session_cache shared:ssl:1m; # ssl_session_timeout 5m; # ssl_ciphers high:!anull:! MD5; # ssl_prefer_server_ciphers on; # location/{# root HTML; # index index.html index.htm; # } #}}
Third, Tomcat + memcached to achieve session replication and synchronization
The problem with Tomcat session replication and synchronization is usually the use of memcached, or Nginx_upstream_jvm_route, an Nginx extension module that implements the function of a Cookie-based session Sticky. If Tomcat too much does not recommend session synchronization, the server synchronization session is very resource-intensive, high concurrency environment prone to the session storm. Please adopt the session solution as appropriate for your application.
The following describes the use of memcached (Memcache-session-manager) to manage the session
1. memcached download installation (slightly)
2. Download the relevant JAR package
There are a lot of jar packages to download, the version of the jar can not be mistaken, I am here to provide a complete set of download of the jar package (has been tested available), need to download the click on the link to download.
3. Configure session Sharing
OK, put the above jar package in the Tomcat/lib directory, modify each Tomcat profile Tomcat\conf\context.xml, <context>...</context> Add the following code between the tags:
A. Configuring Tomcat
Modify Tomcat\conf\server.xml, because I am the local win7 system installed under the 2 tomcat, so to avoid port conflicts, take tomcat-node1 as an example, mainly modify the following places: (Node2 and so on)
<server port= "8005" shutdown= "Shutdown" >
<connector port= "18080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/>
<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "jvm1" >
To modify the Tomcat\conf\context.xml, add the following code between the <context>...</context> tags:
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager" memcachednodes= "<span style= "COLOR: #FF0000;" ><strong>n1:127.0.0.1:11211</strong></span> " sticky=" false " sessionbackupasync=" False " lockingmode=" uripattern:/path1|/path2 " requesturiignorepattern=". *\. ( ICO|PNG|GIF|JPG|CSS|JS) $ " transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory "/>
(Note that the 2 Tomcat configurations are all memecachednodes n1:*)
B. Modifying the application JSP
I am using examples project with Tomcat, and replace the following JSP directly with the D:\nginx-1.8.0\Tomcat\tomcat-node1\webapps\examples\index.html
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ page import= " Java.text.SimpleDateFormat "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Iv. TestingStart Nginx, memcached and 2 tomcat, use the browser to access the http://localhost/examples/, compare the Nginx server returned 2 pages, found 2 times the port is not the same but the session represents Nginx + The Tomcat + Memcached cluster was built successfully.
Reference article:
1. Http://www.tuicool.com/articles/I7ryYf
2. http://blog.csdn.net/liuzhigang1237/article/details/8880752
3. http://www.cnblogs.com/phirothing/archive/2013/12/05/3459814.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Nginx + Tomcat + Memcached cluster