Linux Operations and Maintenance: Nginx-Openresty and Nginx-tomcat

Keywords: Nginx Tomcat Java PHP

I. Openresty

1. Compile and install opesresty
[root@server1 ~]# tar zxf openresty-1.13.6.1.tar.gz 
[root@server1 ~]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ./configure --prefix=/usr/local/lnmp/openresty
[root@server1 openresty-1.13.6.1]# gmake && gmake instal
2. Configuration file
[root@server1 ~]# vim /usr/local/lnmp/openresty/nginx/conf/nginx.conf

 19         upstream memcache {
 20                 server 172.25.12.1:11211;
 21                 keepalive 512;
 22         }

 53         location /memc {
 54         internal;
 55         memc_connect_timeout 100ms;
 56         memc_send_timeout 100ms;
 57         memc_read_timeout 100ms;
 58         set $memc_key $query_string;
 59         set $memc_exptime 300;
 60         memc_pass memcache;
 61         }

 80         location ~ \.php$ {
 81             set $key $uri$args;
 82             srcache_fetch GET /memc $key;
 83             srcache_store PUT /memc $key;
 84             root           html;
 85             fastcgi_pass   127.0.0.1:9000;
 86             fastcgi_index  index.php;
 87        #     fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 88             include        fastcgi.conf;
 89         }
3. Open memcached, php-fpm, nginx
  • Regarding the configuration of memcached and php, welcome to visit my LNMP (Nginx+Mysql+Php) blog, which will not be repeated here. Please forgive me.
[root@server1 ~]# /etc/init.d/memcached start
Starting memcached:                                        [  OK  ]
[root@server1 ~]# /etc/init.d/php-fpm start
Starting php-fpm  done
##Server1 host has already configured nginx, so start openresty here with absolute path
[root@server1 ~]# /usr/local/lnmp/openresty/nginx/sbin/nginx 
4. Physical host testing
  • Nginx does not add a Memcache module, Php has a Memcache module
[kiosk@foundation12 Desktop]$ ab -c 1 -n 1000 http://172.25.12.1/example.php

Time taken for tests:   0.762 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Requests per second:   1728.62 [#/sec] (mean)

[kiosk@foundation12 Desktop]$ ab -c 1 -n 1000 http://172.25.12.1/index.php

Time taken for tests:   2.328 seconds
Complete requests:      1000
Failed requests:        97

Requests per second:    429.56 [#/sec] (mean)
  • Nginx adds Memcahche module, Php is equivalent to "double Memcache"
[kiosk@foundation12 Desktop]$ ab -c 1 -n 1000 http://172.25.12.1/example.php

Time taken for tests:   0.405 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Requests per second:    2468.97 [#/sec] (mean)

[kiosk@foundation12 Desktop]$ ab -c 1 -n 1000 http://172.25.12.1/index.php

Time taken for tests:   0.491 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Requests per second:    2037.92 [#/sec] (mean)
  • By comparison, the speed of Nginx increased from 429.56 [sec] to 2037.92 [sec], and the performance was significantly optimized after adding Memcache module to Nginx. Php performance has also been optimized!

Nginx+tomcat+memcache

Installation of JDK

1. Decompress the source code and set the link (when jdk is updated, update the link will do)
[root@server1 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s jdk1.7.0_79/ java
2. Adding environment variables
[root@server1 local]# vim /etc/profile
##Write the configuration file and source command to load the current shell environment
export  JAVA_HOME=/usr/local/java
export  CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export  PATH=$PATH:$JAVA_HOME/bin

[root@server1 ~]# source /etc/profile
[root@server1 ~]# . /etc/profile   ##Update environment variables in the current shell
[root@server1 ~]# sh /etc/profile  ##Update environment variables in a new shell
##Check for success after loading
[root@server1 local]# echo $JAVA_HOME
/usr/local/java
[root@server1 local]# echo $CLASSPATH
.:/usr/local/java/lib:/usr/local/java/jre/lib
[root@server1 local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin/:/usr/local/java/bin
3. java testing
[root@server1 ~]# vim test.java
##java language format
public class test {
        public  static  void main(String[] args){
                System.out.println("Hello World!");
        }
}
##javac compiles ". java" scripts, Java runs scripts
[root@server1 ~]# javac test.java
[root@server1 ~]# java test
Hello World!

II. Installation of tomcat

1. Decompress the source code and set the link (when tomcat updates, update the link will do)
  • Note: When tomcat tests, close openresty and open nginx for testing
[root@server1 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s apache-tomcat-7.0.37/ tomcat
2. Start tomcat (visit 172.25.12.1:8080 to view the home page, default port 8080)
[root@server1 ~]# cd /usr/local/tomcat/bin/
[root@server1 bin]# ./startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
  • Visit Web pages for testing
3. Test tomcat (access 172.25.12.1:8080/test.jsp crawl system time)
[root@server1 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT
[root@server1 ROOT]# vim test.jsp
[root@server1 ROOT]# cat test.jsp 
the time is: <%=new java.util.Date() %>
4. Modify nginx configuration file, and automatically jump when accessing. jsp file (to ensure the integrity of the home page, modify the default publishing directory of nginx)
[root@server1 ROOT]# vim /usr/local/lnmp/nginx/conf/nginx.conf

 48         location / {
 49            # root   html;   ##Modify the default publishing directory of nginx
 50             root   /usr/local/tomcat/webapps/ROOT;
 51             index  index.php index.html index.htm;
 52         }

 69         location ~ \.jsp$ {
 70             proxy_pass   http://172.25.12.1:8080;
 71         }

[root@server1 ROOT]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 ROOT]# nginx
 ##Visit 172.25.12.1/index.jsp test

3. Realizing the Load Balancing of tomcat

1. Another virtual machine (server 2), configuring java and tomcat
  • With the above steps, java and tomcat need to test ok
2. Serr1 host, configure nginx
root@server1 ROOT]# vim /usr/local/lnmp/nginx/conf/nginx.conf

 20     upstream tomcat {
 21         server 172.25.12.1:8080;
 22         server 172.25.12.2:8080;
 23     }

 76         location ~ \.jsp$ {
 77             proxy_pass   http://tomcat;
 78         }

[root@server1 ROOT]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 ROOT]# nginx -s reload
  • Load Balancing (Page Testing) occurs when accessing 172.25.12.1/test.jsp
3. Session is not synchronized. Solution A: ip_hash algorithm
  • Modify test.jsp (/usr/local/tomcat/webapps/ROOT)
  • Keep the two hosts aligned
[root@server2 ROOT]# cat 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()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
  • Visit http://172.25.12.1/test.jsp When a session is submitted, it refreshes every time it is submitted
  • Data cannot be stored due to asynchronization
  • Modify nginx configuration file and adopt ip_hash algorithm:
[root@server1 ROOT]# vim /usr/local/lnmp/nginx/conf/nginx.conf
 20     upstream tomcat {
 21         ip_hash;
 22         server 172.25.12.1:8080;
 23         server 172.25.12.2:8080;
 24     }
[root@server1 ROOT]# nginx -s reload
  • Access 172.25.12.1/test.jsp, session can be saved
  • Disadvantage: If server 1 host tomcat shutdown, data loss
  • ip_hash can't pass the cdn server; but cookie s can!
  • Visit Home Page Testing
  • The cookie test works the same, but the principle is different.

    4. Session asynchronization, solution B: memcache
  • Note: Serr1 and server2 hosts are identical and configuration files are slightly modified.

  • Take server1 host as an example
[root@server1 lib]# pwd  ##Download module
/usr/local/tomcat/lib

[root@server1 lib]# ls
annotations-api.jar asm-3.2.jar catalina-ant.jar
catalina-ha.jar     catalina.jar    catalina-tribes.jar
ecj-4.2.1.jar       el-api.jar  jasper-el.jar
jasper.jar      jsp-api.jar kryo-1.04.jar
kryo-serializers-0.10.jar   memcached-session-manager-1.6.3.jar
memcached-session-manager-tc6-1.6.3.jar memcached-session-manager-tc7-1.6.3.jar
minlog-1.2.jar      msm-kryo-serializer-1.6.3.jar   reflectasm-1.01.jar
servlet-api.jar     spymemcached-2.7.3.jar      tomcat-api.jar
tomcat-coyote.jar   tomcat-dbcp.jar         tomcat-i18n-es.jar
tomcat-i18n-fr.jar  tomcat-i18n-ja.jar  tomcat-jdbc.jar tomcat-util.jar

[root@server1 lib]# rm -fr memcached-session-manager-tc6-1.6.3.jar
[root@server1 lib]# yum install -y memcached
[root@server1 lib]# /etc/init.d/memcached start
Starting memcached:                                        [  OK  ]

[root@server1 conf]# pwd
/usr/local/tomcat/conf
[root@server1 conf]# vim context.xml

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.12.1:11211,n2:172.25.12.2:11211"
failoverNodes="n2"  ##Note that server1 host is n2, server2 host is n1!!
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>

[root@server1 tomcat]# bin/shutdown.sh

[root@server1 tomcat]# bin/startup.sh

[root@server1 tomcat]# cat logs/catalina.out 
##The following information appears to indicate ok
INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n2] and failover node ids [n1]
5. Visit 172.25.12.1/test.jsp test:
 Server Info: 172.25.12.2 : 8080

ID 51EAC4845CCD9734F0D21988CC4A1938-n2
Session listuser2 = 222
user1 = 
user4 = 444
user3 = 333
user5 = 555
name:
key: 
  • After server 1 host tomcat shutdown, the data is still normal
  • Serr2 host (install telnet)
  • Serr2 host queries Memcache
[root@server2 tomcat]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get 51EAC4845CCD9734F0D21988CC4A1938-n2
VALUE 51EAC4845CCD9734F0D21988CC4A1938-n2 2048 149
Wdcj�dcj�01dcj�+dcj�=#51EAC4845CCD9734F0D21988CC4A1938-n2user5555user2222user1user4444user3333
END

Posted by TNIDBMNG on Thu, 16 May 2019 11:10:20 -0700