redhat6.5 build Nginx+tomcat Load balancer, memcached cache

Source: Internet
Author: User
Tags install openssl memcached tomcat server

Lab Environment:

redhat6.5 version Virtual machine 3 units:

server1:172.25.44.10, Memory 1024m,cpu dual Core

server2:172.25.44.20, Memory 512m,cpu single core

server3:172.25.44.30, Memory 512m,cpu single core

These three virtual machines iptables are off and SELinux is disabled state.

the difference between Apache and Nginx :

Nginx's advantages over Apache: Lightweight, also Web services, Nginx consumes less memory and resources than Apache. Nginx processing request is asynchronous non-blocking, and Apache synchronous blocking type, Nginx can keep low resource consumption and high performance, nginx load capacity is much higher than Apache.

Apache compared to the advantages of Nginx: Rewrite features more powerful than Nginx rewrite, modules, in general, the need for performance of Web services with Nginx, if not required performance for stability, with Apache.

asynchronous non-blocking and synchronous-blocking modes of operation differ :

the difference between synchronous and asynchronous : Synchronization is to send a command, the server to process, waiting for the server to pass the return value to you before the end, like you call to reserve a hotel room, the waiter to you to find the room, in this search process you need to wait until the waiter gives you a reply (return the result).

Asynchronous is to send a command, the server to process, to the server will return the value of delivery back, do not need to wait, like you call a hotel room, waiter directly tell you I check ah, check to tell you, and then directly hang up the phone (do not return the results), and so he checked, and then volunteered to call you.

the difference between blocking and non-blocking : In the example above, if it's a blocking call, you'll always be doing nothing, holding the phone until you know there's no room. If the call is non-blocking, you don't care if the waiter tells you, you play, but you also have to check the following waiter for a few minutes to return your results.

Experimental process:

On the Server1 virtual machine, perform the following actions:

Download the nginx1.8.1 version of the TAR package from the Web site

Tar zxf nginx-1.8.1.tar.gz #解压安装包

CD nginx-1.8.1

Useradd-u 800-m-d/usr/local/nginx-s/sbin/nologin nginx #新建nginx用户, specifying its uid800

The compiler needs to turn off debug mode, configure file for the auto/cc gcc file for that directory, and also need to change Nginx version information, config file for that directory's Src/core nginx.h file.

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/83/F3/wKiom1eBBzKgxZ85AACTxMXlfBc460.png-wh_500x0-wm_3 -wmp_4-s_1060691989.png "title=" screenshot from 2016-07-10 06:15:43.png "alt=" Wkiom1ebbzkgxz85aactxmxlfbc460.png-wh _50 "/>

Compile nginx, need C compile

Yum Install Gcc-y

What's missing in the process of compiling?

Yum Install Pcre-devel.x86_64-y

Yum Install Openssl-devel.x86_64-y

Make && make install

Edit Vim. Bash_profile add/usr/local/nginx/sbin to the variable

Vim/usr/local/nginx/conf/nginx.conf

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/83/F3/wKiom1eBC8KhnuJZAABeQGznCPE932.png-wh_500x0-wm_3 -wmp_4-s_1893657798.png "title=" screenshot from 2016-07-10 06:35:17.png "alt=" Wkiom1ebc8khnujzaabeqgzncpe932.png-wh _50 "/>

Server1 this virtual CPU is dual core, so worker_processes 2;

CPU bundle, Worker_cpu_affinity 01 10; # #01为第一个CPU, 10 is the second CPU.

The event operation mode selects Epoll (asynchronous non-blocking) mode.

On Server2 and Server3 virtual machines, do the following: 19

Yum Install Httpd-y

/ETC/INIT.D/HTTPD start

Create a test Web file on Server2 echo server2 >/var/www/html/index.html

Create a test Web file on Server3 echo server3 >/var/www/html/iindex.html

On the Server1 virtual machine:

Vim/usr/local/nginx/conf/nginx.conf

Line 19th upstream Westos {

Row 20th Server 172.25.44.20:80;

Row 21st Server 172.25.44.30:80;

Line 22nd}

49th, 50 lines of comments

Line 51st Proxy_pass Http://westos;

Nginx-t #检查有没有错误

Nginx-s Reload #刷新nginx

Browser access 172.25.44.10, refresh Web pages, Server2 and Server3 polling display

Vim/usr/local/nginx/conf/nginx.conf

Line 19th upstream Westos {

Line 20th Ip_hash; #捆绑IP, which causes the page to be refreshed without polling.

Row 21st Server 172.25.44.20:80;

Row 22nd Server 172.25.44.30:80;

Line 23rd}

Nginx-t

Nginx-s Reload

Open the browser to test, if one client hangs up, will display the contents of another client, the hanging client is good again, does not automatically reverse the display of the bad client information.

sticky: Using nginx sticky to implement cookie-based load balancing

Recompile the nginx and add the sticky module.

Nginx-s Stop #先将nginx停掉

Download the nginx-sticky-module-1.0.tar.gz compressed package to the/MNT directory

Tar zxf nginx-sticky-module-1.0.tar.gz-c nginx-1.8.1

cd/mnt/nginx-1.8.1

Make clean

./configure--prefix=/usr/local/nginx--with-http_ssl_module--user=nginx--group=nginx--add-module= nginx-sticky-module-1.0/

Make && make install

Vim/usr/local/nginx/conf/nginx.conf

Line 20th Sticky;

Nginx-t

Nginx

Note: Sticky no_fallback; When a client hangs up, the Refresh page does not display the information of another client, and the page error message is displayed directly.

Server 172.25.44.20:80 weight=3;

Server 172.25.44.30:80; # #weight的作用是一种权重关系, refresh the page display Server2 three times to show Server3 once. Backup is a standby relationship, and if one client hangs up and another client works on top, down is good for client service, but the server is down so it can't be polled.

Tomcat:tomcat server is a free open source Web application server, which belongs to the lightweight application server and is the server for developing and debugging JSP programs.

On Server2 and Server3 virtual machines:

Tomcat Installation :

SH jdk-6u26-linux-x64.bin

MV jdk1.6.0_26//usr/local

Vim/etc/profile
Export JAVA_HOME=/USR/LOCAL/JDK
Export classpath=: $JAVA _home/lib
Export path= $PATH: $JAVA _home/bin

Source/etc/profile
Ln-s/usr/local/jdk1.6.0_26/java

Tar zxf apache-tomcat-7.0.37.tar.gz-c/usr/local

Ln-s/usr/local/apache-tomcat-7.0.37 Tomcat

vim/usr/local/tomcat/webapps/root/test.jsp

Server2 the time is: <%=new java.util.Date ()%> #在server3上写server3 to facilitate our experimental testing

Cd/usr/local/tomcat

bin/start.sh

On the Server1 virtual machine, perform the following actions:

Vim/usr/local/nginx/conf/nginx.conf

Line 19th upstream Westos {

Line 20th Sticky;

Row 21st Server 172.25.44.20:8080;

Row 22nd Server 172.25.44.20:8080;

Line 23rd}

50th line root HTML;

Line 51st index index.html index.htm;

65th Row Location ~ \.jsp {# #将jcp页面交给tomcat处理, static and dynamic separation

Line 66th Proxy_pass Http://westos;

Line 67th}

Nginx-t

Nginx-s Reload

Browser detection

memcache: Memcache is a high-performance distributed cache system. It can be used to save some frequently accessed data. Access data operations can be performed directly on the memcached server via Telnet.

Tomcat uses cross-storage to store data on memcached, which has the advantage of not losing session sessions and avoiding single points of failure when T1 and M1 are simultaneously collapsed.

On Server2 and Server3 virtual machines:

Yum Install Memcached-y

/etc/init.d/memcached start

/usr/local/tomcat/bin/shutdown.sh

Put the following software in the/usr/local/tomcat/lib directory

Kryo-1.03.jar
Kryo-serializers-0.8.jar
Memcached-2.5.jar
Memcached-session-manager-1.5.1.jar

Memcached-session-manager-tc7-1.5.1.jar
Minlog-1.2.jar
Msm-kryo-serializer-1.5.1.jar
Reflectasm-0.9.jar

Vim/usr/local/tomcat/conf/context.xml

        <context>
             
        <manager classname= " De.javakaffee.web.msm.MemcachedBackupSessionManager "
         Memcachednodes= "n1:172.25.44.20:11211,n2:172.25.44.30:11211"
         failovernodes= "N1"
         #在 Node2 This is set to "N2"
         requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "
        transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory "
         />
        </context>

The following is the test page, saved to/usr/local/tomcat/webapps/root/test.jsp

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "java.util.*"%>
<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>

/usr/local/tomcat/bin/start.sh #开启服务

Access to http://172.25.44.10/test.jsp, when different host accesses are scheduled to be processed on different tomcat instances
Requests from the same host are handled by the same Tomcat instance, where you drop the currently responding Tomcat real
Example, Nginx automatically dispatches the user's request to another Tomcat instance, and the session is not discarded.






redhat6.5 build Nginx+tomcat Load balancer, memcached cache

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.