Apache + tomcat implements Load Balancing cluster and session sharing, and tengine + tomcat achieves dynamic and static web Separation

Source: Internet
Author: User

1. tomcat Introduction

The Tomcat server is a free open-source Web application server, which is a lightweight application server. It is widely used in small and medium systems and concurrent users, is the first choice for developing and debugging JSP programs.
Tomcat provides a Jasper compiler to compile JSP into the corresponding Servlet. The TomcatServlet engine usually works with Apache or other Web servers. In addition to debugging during development and users who have little requirements on speed and transaction processing, Tomcat is rarely used as a Web server. However, as the version is updated, more and more users are using it as a Web server alone in environments with high speed and reliability requirements. Because Tomcat is developed in Java, it can run on any different operating system with JVM installed.


Ii. experiment environment

System Environment: centos6.4-i386

Jdk: jdk-7u40-linux-i586.rpm

Tomcat: apache-tomcat-7.0.42

Nginx: tengine-1.5.1

Apache: httpd-2.4.2

3. Build an experiment environment

1. Install jdk

#rpm -ivh jdk-7u40-linux-i586.rpm#vim /etc/profile.d/java.shexport JAVA_HOME=/usr/java/latestexport PATH=$JAVA_HOME/bin:$PATH# . /etc/profile.d/java.sh# java -version

2. install tomcat

# tar xf apache-tomcat-7.0.33.tar.gz -C /usr/local/# cd /usr/local/# ln -s apache-tomcat-7.0.33 tomcat# vim /etc/profile.d/tomcat.shexport CATALINA_HOME=/usr/local/tomcatexport PATH=$CATALINA_HOME/bin:$PATH# . /etc/profile.d/tomcat.sh

Add service script

[root@stu_yxm18 ~]# vim /etc/rc.d/init.d/tomcat#!/bin/sh# Tomcat init script for Linux.## chkconfig: 2345 96 14JAVA_OPTS='-Xms64m -Xmx128m'JAVA_HOME=/usr/java/latestCATALINA_HOME=/usr/local/tomcatexport JAVA_HOME CATALINA_HOMEexec $CATALINA_HOME/bin/catalina.sh $*

Start tomcat

[root@stu_yxm18 ~]# service tomcat startUsing CATALINA_BASE:   /usr/local/tomcatUsing CATALINA_HOME:   /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME:        /usr/java/latestUsing CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

650) this. width = 650; "title =" Unnamed 2.png "alt =" 092433752.png" src = "http://www.bkjia.com/uploads/allimg/131227/2015036450-0.png"/>

Note: The installation of apache and nginx is not demonstrated here. We strongly recommend that you compile and install apache and nginx !!!

4. Configure tomcat to run a simple jsp program

# Cd/usr/local/tomcat/conf/***** back up the master configuration file ***** # cp server. xml server. xml. bak # vim server. xml ******* modify the main configuration file as follows ************ Add a VM ****** <Host name = "www.yxm.com" appBase = "/usr/local/tomcat/yxmtctest" unpaclWARs = "true" autoDeploy = "true"> <Valve className = "org. apache. catalina. valves. accessLogValve "directory =" logs "prefix =" www. yxm_access_log. "suffix = ". txt "pattern =" % h % l % u % t & quot; % r & quot; % s % B "/> <Context path =" "docBase ="/usr/local/tomcat/yxmtctest "/> </Host> ****** modify the service port for 80 * <Connector port = "80" protocol = "HTTP/1.1" connectionTimeout = "20000" redirectPort = "8443"/> * default VM: pay attention to the corresponding ***** <Engine name = "Catalina" defaultHost = "www.yxm.com">

Provides a jsp program

# Cd/usr/local/tomcat/# mkdir yxmtctest # [root @ stu_yxm18 tomcat] # cd yxmtctest/# mkdir-p WEB-INF/{class, lib} # vim index. jsp <% @ page language = "java" %> 

650) this. width = 650; "title =" Unnamed 8.png "alt =" 095307965.png" src = "http://www.bkjia.com/uploads/allimg/131227/2015035Z2-1.png"/>

5. Apache performs reverse proxy and proxy for backend tomcat

Compile and install httpd-2.4.2

#tar xf httpd-2.4.2.tar.bz2#ln -sv httpd-2.4.2 httpd#cd httpd#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util  --enable-proxy --enable-proxy-http --enable-proxy-ajp#make && make install

Modify the main configuration file

# vim /etc/httpd/httpd.confInclude /etc/httpd/extra/mod_proxy.conf# vim /etc/httpd/extra/mod_proxy.confProxyVia OnProxyRequests OffProxyPreserveHost Off<Proxy *>Order allow,denyAllow from all</Proxy>ProxyPass  /  ajp://172.16.15.23:8009/ProxyPassReverse  /  ajp://172.16.15.23:8009/

Note:

<1> ProxyVia {On | Off | Full | Block}: controls whether to use Via in the http header. On indicates that the Via is added to each request and Response Message.

<2> ProxyRequests {On | Off}: whether to enable the apache forward proxy function.

<3> ProxyPreserveHost {On | Off}: If this function is enabled, the proxy will send the Host: line in the user request message to the backend server, instead of using the server address specified by ProxyPass. To support virtual hosts in reverse proxy, you must enable this option.

<4> ProxyPass/ajp: // 172.16.15.23: 8009/##### the ajp protocol is used.
<5> ProxyPassReverse/ajp: // 17..15.23: 8009/

650) this. width = 650; "title =" Unnamed 6.png "alt =" 105508166.png" src = "http://www.bkjia.com/uploads/allimg/131227/20150325B-2.png"/>

6. Apache + tomcatMod_proxy-based moduleBuild a tomcat Load Balancing Cluster

# vim /etc/httpd/extra/mod_proxy.confProxyVia OffProxyRequests OffProxyPreserveHost Off<Proxy balancer://yxm>BalancerMember ajp://172.16.15.18:8009 loadfactor=1BalancerMember ajp://172.16.15.23:8009 loadfactor=1ProxySet lbmethod=bytraffic</Proxy>ProxyPass  /  balancer://yxm/    stickysession=JSESSIONIDProxyPassReverse  /  balancer://yxm/

Loadfactor = 1 # weight

ProxySetlbmethod = bytraffic # Scheduling Algorithm

Add a service on another tomcat node: The Master configuration file is identical, and the jsp program is different to verify the effect of Server Load balancer.

650) this. width = 650; "title =" Unnamed 8.png "alt =" 110042519.png" src = "http://www.bkjia.com/uploads/allimg/131227/20150335M-3.png"/>

650) this. width = 650; "title =" Unnamed 7.png "alt =" 103643499.png" src = "http://www.bkjia.com/uploads/allimg/131227/20150330G-4.png"/>

7. Separation of web dynamic and static data using tengine + tomcat

Note: The installation of tengine does not demonstrate compilation and installation here )!!!

Modify nginx main configuration file

[root@nginx ~]# vim /etc/nginx/nginx.confuser  nginx nginx;worker_processes  2;worker_rlimit_nofile 51200;#error_log  logs/error.log;#pid        logs/nginx.pid;events {use epoll;worker_connections  51200;}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;client_max_body_size 20m;client_header_buffer_size 16k;large_client_header_buffers 4 16k;sendfile        on;tcp_nopush     on;keepalive_timeout  65;gzip  on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_proxied   any;gzip_http_version 1.1;gzip_comp_level 3;gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;proxy_temp_path   /tmp/proxy_temp;proxy_cache_path  /tmp/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=3g;proxy_connect_timeout    50;proxy_read_timeout       600;proxy_send_timeout       600;proxy_buffer_size        128k;proxy_buffers           16 256k;proxy_busy_buffers_size 512k;proxy_temp_file_write_size 1024m;proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;server {listen       80;server_name  www.yxm.com ;access_log  logs/host.access.log  main;location / {proxy_pass http://172.16.15.14;}location ~* \.(|html|htm|jpg|jpeg|png|gif|bmp|swf|ico)$ {proxy_pass http://172.16.15.18;}location ~* \.(jsp|css|do|php)$ {proxy_pass http://172.16.15.23;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}

Restart the nginx Service

8. session sharing through Apache + tomcat

Configure the backend tomcat server IP Address: 172.16.15.18

# Vim/usr/local/tomcat/conf/server. xml <Engine name = "Catalina" defaultHost = "www.yxm.com"> # Note the location where you add <Cluster className = "org. apache. catalina. ha. tcp. simpleTcpCluster "channelSendOptions =" 8 "> <Manager className =" org. apache. catalina. ha. session. deltaManager "expireSessionsOnShutdown =" false "yylistenersonreplication =" true "/> <Channel className =" org. apache. catalina. tribes. group. groupChannel "> <Membership className =" org. apache. catalina. tribes. membership. mcastService "address =" 228.15.15.15 "# multicast address, the two nodes must be consistent. port = "45564" frequency = "500" dropTime = "3000"/> <Cycler className = "org. apache. catalina. tribes. transport. nio. nioReceiver "address =" 172.16.15.18 "# IP address, you can use autoport = "4000" autoBind = "100" selectorTimeout = "5000" maxThreads = "6"/> <Sender className = "org. apache. catalina. tribes. transport. replicationTransmitter "> <Transport className =" org. apache. catalina. tribes. transport. nio. pooledParallelSender "/> </Sender> <Interceptor className =" org. apache. catalina. tribes. group. interceptors. tcpFailureDetector "/> <Interceptor className =" org. apache. catalina. tribes. group. interceptors. messageDispatch15Interceptor "/> </Channel> <Valve className =" org. apache. catalina. ha. tcp. replicationValve "filter =" "/> <Valve className =" org. apache. catalina. ha. session. jvmRouteBinderValve "/> <Deployer className =" org. apache. catalina. ha. deploy. farmWarDeployer "tempDir ="/tmp/war-temp/"deployDir ="/tmp/war-deploy/"watchDir ="/tmp/war-listen/"watchEnabled =" false" /> <ClusterListener className = "org. apache. catalina. ha. session. jvmRouteSessionIDBinderListener "/> <ClusterListener className =" org. apache. catalina. ha. session. clusterSessionListener "/> </Cluster>

Create a web. xml file in/usr/local/tomcat/yxmtctest/WEB-INF/

Vim/usr/local/tomcat/yxmtctest/WEB-INF/web. xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><distributable/></web-app>~

Copy the preceding configuration to the 172.16.15.23tomcat node and change the IP address.

#scp /usr/local/tomcat/conf/server.xml  172.16.15.23:/usr/local/tomcat/conf#scp /usr/local/tomcat/yxmtctest/WEB-INF/web.xml 172.16.15.23:/usr/local/tomcat/yxmtctest/WEB-INF/#server tomcat stop#server tomcat start

650) this. width = 650; "title =" Unnamed 8.png "alt =" 145655370.png" src = "http://www.bkjia.com/uploads/allimg/131227/2015035104-5.png"/> 650) this. width = 650; "title =" Unnamed 9.png "alt =" 145719741.png" src = "http://www.bkjia.com/uploads/allimg/131227/2015032449-6.png"/>

Check to see that the two sessionids are exactly the same !!! Session sharing successful

This article is from the "cloud overlooking" blog, please be sure to keep this source http://dreamwolf.blog.51cto.com/6365503/1304987

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.