nginx+tomcat+memcached Configuration + Script

Source: Internet
Author: User
Tags nginx reverse proxy nginx load balancing
This paper studies the configuration of the next nginx+tomcat+memcached, exercises the writing of the next script, and references several articles, in which it is noted that there is something wrong.

System environment:
RHEL6.5 x64
Iptables-f & SELinux is disabled

Host role:
Node1:192.168.122.101:nginx Tomcat memcached
Node2:192.168.122.102:tomcat memcached

Https://code.google.com/memcached-session-manager
Session Management of Memcached

Nginx do reverse proxy two Tomcat, with memcached synchronization session, to prevent data loss

TOMCAT1 stores the session on Memcacted2. Tomcat and memcached use cross-storage, and T1 stores data on M1 only when M2 is unavailable (M1 T1 failovernode). This way you can avoid a single point of failure. This enables high availability of applications.

Note: Tomcat on both node should be exactly the same

1. Configuring the Tomcat environment on two node hosts

#./jdk-6u32-linux-x64.bin# mv jdk1.6.0_32/ /usr/local/lnmp/jdk#vim /etc/profile              编写环境变量export JAVA_HOME=/usr/local/lnmp/jdkexport CLASSPATH=:$JAVA_HOME/lib:$JAVA_HOME/jre/libexport PATH=$PATH:$JAVA_HOME/bin#source /etc/profile测试java能否正常工作#vim test.javapublicclasstest{publicstaticvoid main(String[] args){System.out.println("Hello!");  }}#javac test.java          编译#java test               执行后出现Hello!说明java环境配置好

Installing the Tomcat server

#tar zxf apache-tomcat-7.0.37.tar.gz      解压就能用,不需要编译#mv apache-tomcat-7.0.37 tomcat#/TOMCAT_ROOT_DIR/tomcat/webapps/ROOT                tomcat的默认发布目录#/TOMCAT_ROOT_DIR/tomcat/bin/startup.sh(shutdown.sh)    tomcat默认的启动和关闭脚本

Tomcat turns on port 8080 by default,

Test http://192.168.122.101:8080 access to the Tomcat default test page

#cd tomcat/webapps/ROOT#cat test.jsp           #测试页this time is: <%=new java.util.Date()%>

Test access; http://192.168.122.101:8080 Display the current time

Tomcat accesses port 8080, using Nginx reverse proxy.

To publish a JSP Dynamic Web page using Nginx:

Nginx configuration file:

Cat/usr/local/nginx/conf/nginx.conf

#user nginx nginx;Worker_processes4;#error_log Logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {use Epoll; Worker_connections1024x768;} HTTP {upstream Tomcat {#两台tomcat负载均衡Sticky#session同步, the module for NginxServer192.168. 0. 1:8080;Server192.168. 0. 2:8080;    } include Mime.types; Default_type Application/octet-stream;#log_format main ' $remote _addr-$remote _user [$time _local] "$request" '# ' $status $body _bytes_sent ' $http _referer '#access_log Logs/access.log main;Sendfile on;#tcp_nopush on;#keepalive_timeout 0;Keepalive_timeout $;#gzip on;Server{Listen the; server_name localhost;#charset koi8-r;#access_log Logs/host.access.log main;Location/{root HTML; Proxy_pass http://tomcat;IndexIndex. htmlIndex. htm; }#error_page 404/404.html;# REDIRECT Server error pages to the static page/50x.htmlError_page -502503504/ -x.html; Location =/ -x.html {root HTML; } location ~ \.jsp$ {Proxy_pass http://tomcat;}}nginx-t && Nginx-s Reload

Visit: http://192.168.122.101/test.jsp test

Synchronize the contents of both Tomcat and modify the Java environment variable/etc/profile

Test: http://192.168.122.102:8080/test.jsp

Two node (Tomcat) is ready.

2. Publish Web pages in Nginx environment on Node1

Using Nginx's load balancing function,

Embodied in the nginx.conf:

upstream tomcat-lb {  server192.168.122.101:8080;  server192.168.122.102:8080;}location~ \.jsp$ {  proxy_passhttp://tomcat-lb;}nginx -t && nginx -s reload

Test: http://192.168.122.101/test.jsp
Load balancing for two hosts (access to the node where Nginx resides)

Problem: Nginx load balancing is implemented in the background, but when a user refreshes the data, the data always changes, just imagine, in the dynamic page, if the user submits the data refresh and did not submit to the server, and must re-fill the table, which will create a bad user experience.

Solution: Add a sticky module to nginx. (need to recompile nginx)

To re-build the module in Nginx:

Nginx-sticky-modules.tar.gz (unzip on the line)

#tar zxf nginx-sticky-modules.tar.gz -C /root/nginx-1.4.2#cd nginx-1.4.2/#make clean#./configure --prefix=/usr/local/nginx --add-module=$NGINX_PKG_DIR/$DIR/nginx-sticky-module-1.0 --with-http_ssl_module --with-http_stub_status_module#make && make install#vim nginx.confupstream linux {  sticky;   192.168.122.101:8080192.168.122.102:8080 ;}

Test: Http://192.168.122.101/test.jsp found that refresh does not load back and forth (each user sees not a tomcat data, but is transparent to the user)

3.nginx load Tomcat JSP, you need to resolve session sharing:

Cache (user) backend data using Memcache, but with the idea of solving a single point of failure, two memcache can be used as back-end loads.

memcached default port 11211, backend uses cross storage (Tomcat will synchronize session, session automatically looks for stored memcached, but the default is cross-storage, when a memcached server breaks down, Tomcat will be stored on the surviving memcached server)

As long as Tomcat doesn't fall, all the data is there.

But when Memcached goes down, Tomcat accesses the surviving memcached.

There are 4 official recommendations for the session's serialization scheme:

    1. Java serialization

    2. Msm-kryo-serializer

    3. Msm-javolution-serializer

    4. Msm-xstream-serializer

One of the best performance is Kryo, we use Kryo to do

mecached Server Node1 and Node2

#yum install memcached -y#/etc/init.d/memcached start

memcached Default on Port 11211

Test access: Telnet localhost 11211

Stats View Status
Set User 0 0 3 Store new value (add replace)
Get user Gets the value

Configure two memcached servers (tomcat), download the jar package (must support the relevant Java program)
A package that provides a test environment, which can be extracted by:
Link: Http://pan.baidu.com/s/1mgIF9NU Password: N5WU

Asm-3.2.jar
Couchbase-client-1.2.2.jar
Kryo-1.03.jar
Kryo-serializers-0.11.jar
Memcached-session-manager-1.6.5.jar
Memcached-session-manager-tc7-1.6.5.jar
Minlog-1.2.jar
Msm-kryo-serializer-1.6.5.jar
Reflectasm-0.9.jar
Spymemcached-2.10.3.jar

Node1 and Node2 Synchronizing content

#cd tomcat/lib/#mget jar/*    下载tomcat的session共享管理包#cd tomcat/conf/# vim context.xml
  
   
    
   "de.javakaffee.web.msm.MemcachedBackupSessionManager"memcachedNodes=
   
    "n1:192.168.122.101:11211,n2:192.168.122.102:11211"failoverNodes=
   
    "n1"    #tomcat2需要写成n2requestUriIgnorePattern=
   
    ".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass=
   
    "de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
   
    #tomcat/bin/shutdown.sh #重启tomcat,以识别memcached-session-manager
   
    # tail -f logs/catalina.out    #默认日志INFO: MemcachedSessionService finished initialization, sticky 
   
    true, operation timeout 
   
    1000, with node ids [n2] and failover node ids [n1]
  
   

Normal start

Edit Test file: (JSP test page that submits user information)

# Vim Tomcat/webapps/root/test.jsp<%@ page contenttype="text/html; CHARSET=GBK " %><%@ page import="java.util.*" %><html><head><title>Cluster App Test 
     title
 > 
     Head
 ><body>Server Info:<%out.println (request. GETLOCALADDR () + ":" + request. Getlocalport () +  "
");%><%out.println ("
ID " + session.getid () +"
"); String dataname = request. GetParameter ("Dataname"); if (Dataname! = null && dataname.length () > 0) {String datavalue = request. GetParameter ("DataValue"); Session.setattribute ( Dataname, DataValue);} Out.print ("Session list"); Enumeration E = Session.getattributenames (); while (E.hasmoreelements ()) {string name = (string) e.nextelement (); String value = Session.getattribute (name). toString (); out.println (name + "=" + value+"
"); SYSTEM.OUT.PRINTLN (name + "=" + value);} %><formAction="test.jsp"method="POST">Name<inputtype=textsize== ="Dataname" ><br>Key<inputtype=textsize=thename=" DataValue "><br><inputtype=Submit> Form > Body > HTML >

Session sharing complete: Two Tomcat and memcached do the same configuration (JDK,MEMCACHE,TOMCAT)

Test:

http://192.168.122.101/test.jsp
Session sharing with one Tomcat and another memcached

It doesn't matter if any tomcat or memcached hangs up.

Session will record and keep the user's data information

I also wrote a button to install the script, there are some problems, but also share, I hope the great God can guide ~ ~

First, run on the machine with Nginx:

#!/bin/bashSetenforce0>/dev/nulliptables-f >/dev/nullsed-i' s/^selinux=enforcing/selinux=disabled/g '/etc/selinux/config############### Nginx + tomcat + memcachedDir_now= 'pwd' Ipaddr_ntm=' 192.168.122.101 'ipaddr_tm=' 192.168.122.102 'Nginx_pkg_dir='/root/one_key_install 'Nginx_pkg_name=' nginx-1.6.1.tar.gz 'Nginx_dir='/usr/local/nginx 'Tomcat_pkg_dir='/root/one_key_install 'Tomcat_pkg_name=' apache-tomcat-7.0.37.tar.gz 'Tomcat_dir='/usr/local/tomcat 'Sticky_pkg_dir='/root/one_key_install 'Sticky_pkg_name=' nginx-sticky-module-1.0.tar.gz 'Jdk_bin_dir='/root/one_key_install 'Jdk_bin_name=' Jdk-6u32-linux-x64.bin '################### nginx + sticky InstallCD$NGINX _pkg_dirTar zxf$NGINX _pkg_nameDir= ' Ls-f | grep/$ | grep Nginx | Awk-f'/'' {print '} '' Tar zxf$STICKY _pkg_dir/$STICKY _pkg_name-C$NGINX _pkg_dir/$DIRSed-i' s/^cflags= ' $CFLAGS-g "/#CFLAGS =" $CFLAGS-G "/g '/$NGINX _pkg_dir/$DIR/auto/cc/gccsed-i' s/^ #define nginx_ver \ "nginx\/\" nginx_version/#define NGINX_VER \ "nginx\/\"/g '/$NGINX _pkg_dir/$DIR/src/core/nginx.hyum Install gcc pcre-devel openssl-devel-yCD$NGINX _pkg_dir/$DIR./configure--prefix=/usr/local/nginx--add-module=$NGINX _pkg_dir/$DIR/nginx-sticky-module-1.0--with-http_ssl_module--with-http_stub_status_modulemake && Make Installln- S/usr/local/nginx/sbin/nginx/usr/local/sbin/useradd-m- D/usr/local/nginx/- S/sbin/nologin NginxCD$DIR _nowRm- F/USR/LOCAL/NGINX/CONF/NGINX.CONFCP Nginx.conf.exp/usr/local/nginx/conf/nginx.confsed-i"S/server 192.168.0.1:8080;/server ${ipaddr_ntm}: 8080;/g"/usr/local/nginx/conf/nginx.confsed-i"S/server 192.168.0.2:8080;/server ${ipaddr_tm}: 8080;/g"/usr/local/nginx/conf/nginx.conf##################### Tomcat InstallCD$JDK _bin_dirSh$JDK _bin_nameDir= ' Ls-f | grep/$ | grep JDK | Awk-f'/'' {print '} '' MV$DIR/usr/local/mv/usr/local/$DIR/usr/local/jdkEcho"Export java_home=/usr/local/jdkexport classpath=:\ $JAVA _home/libexport path=\ $PATH: \ $JAVA _home/bin">>/etc/profileCD$TOMCAT _pkg_dirTar zxf$TOMCAT _pkg_name-c/usr/localCD/usr/localmv/usr/local/' ls | grep Tomcat '/usr/local/tomcatln- S/usr/local/tomcat/bin/startup.sh/usr/local/sbin/tomcat-startln- S/usr/local/tomcat/bin/shutdown.sh/usr/local/sbin/tomcat-stopCD$DIR _nowTar zxf kryo_pkgs.tar.gzCDKRYO_PKGSCP */usr/local/tomcat/libCD$DIR _nowRm- F/USR/LOCAL/TOMCAT/CONF/CONTEXT.XMLCP Context.xml.exp/usr/local/tomcat/conf/context.xmlCD$DIR _nowRm- F/USR/LOCAL/TOMCAT/CONF/CONTEXT.XMLCP Context.xml.exp/usr/local/tomcat/conf/context.xmlsed-i"s#memcachednodes=\" n1:192.168.0.1:11211,n2:192.168.0.2:11211\ "#memcachedNodes =\" N1:${ipaddr_ntm} : 11211,n2:${ipaddr_tm}: 11211\ "#"/usr/local/tomcat/conf/context.xml#################### memcached InstallYum Install Memcached-y#################### Start Services/etc/init.d/memcached startSource/etc/profiletomcat-startnginx

Second, run on a machine that's only Tomcat and memcached.

#!/bin/bashSetenforce0>/dev/nulliptables-f >/dev/nullsed-i' s/^selinux=enforcing/selinux=disabled/g '/etc/selinux/configdir_now= 'pwd' Ipaddr_ntm=' 192.168.122.101 'ipaddr_tm=' 192.168.122.102 'Tomcat_pkg_dir='/root/no_ngx 'Tomcat_pkg_name=' apache-tomcat-7.0.37.tar.gz 'Tomcat_dir='/usr/local/tomcat 'Sticky_pkg_dir='/root/no_ngx 'Sticky_pkg_name=' nginx-sticky-module-1.0.tar.gz 'Jdk_bin_dir='/root/no_ngx 'Jdk_bin_name=' Jdk-6u32-linux-x64.bin 'CD$JDK _bin_dirSh$JDK _bin_nameDir= ' Ls-f | grep/$ | grep JDK | Awk-f'/'' {print '} '' MV$DIR/usr/local/mv/usr/local/$DIR/usr/local/jdkEcho"Export java_home=/usr/local/jdkexport classpath=:\ $JAVA _home/libexport path=\ $PATH: \ $JAVA _home/bin">>/etc/profileCD$TOMCAT _pkg_dirTar zxf$TOMCAT _pkg_name-c/usr/local/CD/usr/localmv/usr/local/' ls | grep Tomcat '/usr/local/tomcatCD$DIR _nowTar zxf kryo_pkgs.tar.gzCDKRYO_PKGSCP */usr/local/tomcat/libCD$DIR _nowRm- F/USR/LOCAL/TOMCAT/CONF/CONTEXT.XMLCP context.xml.exp/usr/local/tomcat/conf/mv/usr/local/tomcat/conf/ Context.xml.exp/usr/local/tomcat/conf/context.xmlsed-i"s#memcachednodes=\" n1:192.168.0.1:11211,n2:192.168.0.2:11211\ "#memcachedNodes =\" N1:${ipaddr_ntm} : 11211,n2:${ipaddr_tm}: 11211\ "#"/usr/local/tomcat/conf/context.xmlsed-i' s/failovernodes= ' N1 "/failovernodes=" N2 "/ "/usr/local/tomcat/conf/context.xmlyum Install memcached-y/etc/init.d/memcached StartSource/etc/profileln- S/usr/local/tomcat/bin/startup.sh/usr/local/sbin/tomcat-startln- S/usr/local/tomcat/bin/shutdown.sh/usr/local/sbin/tomcat-stoptomcat-start

The two scripts have the same problem, and after execution it is discovered

source /etc/profile

This sentence is not executed, must be executed manually, has been baffled its solution, I hope someone can answer, thank you ~ ~

The above describes the nginx+tomcat+memcached configuration + script, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.