One server implementation runs two Tomcat and load balancing with Nginx

Source: Internet
Author: User
Tags curl openssl sendfile centos openldap tomcat tomcat server nginx reverse proxy

In order to achieve server upgrades without affecting user use, it is necessary to configure two tomcat on one or more servers. The following procedure describes configuring two tomcat on the same server.

The steps are as follows:

1, copy the original tomcat to another directory, such as: tomcat-8090, clear Logs Directory
2, modify the Server.xml file in Tomcat, the shutdown and connector port modified to another value, such as 8006/8090
3. Modify startup.sh and shutdown.sh files and add in the first line:
Export catalina_home=/opt/app/tomcat-8090
4. Start Tomcat
5. Configure load Balancing in Nginx:
Nginx configuration file, adding:
Upstream Tomcatcluster {
Server <ip>:8080;
Server <ip>:8090;
}
Server
{
Listen 80;
server_name <ip> < domain name > ...;
Index ...;
Root ...;
Location/{
Location ~*/< Project name >/{
Proxy_pass Http://tomcatCluster;
}
}
}

Restart Nginx:service nginx Restart

6. Is the test request forwarded to two Tomcat in turn?


implementation of Tomcat load balancing with Nginx reverse proxy in CentOS 6.5 system

1. View the kernel and system parameters and version of the current system.

[Root@node1 ~]# Uname-a
Linux node1 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 03:15:09 UTC 2013 x86_64 x86_64 x86_64 gnu/linux
[Root@node1 ~]# Cat/etc/issue
CentOS Release 6.5 (Final)
Kernel \ r \m

2. Install Nginx.
1 Install the GCC compiler and related tools and dependencies library.
[root@node1 ~]# yum-y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libx ML2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel Curl Curl-de Vel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel OpenSSL openssl-devel openldap openldap-devel nss_ldap o Penldap-clients openldap-servers Pcre

2 Compile and install Pcre Library.
PCRE (Perl compatible Regular Expressions) is a Perl library that includes Perl-compatible formal expressions. The main function of Pcre is to enable Nginx to support HTTP rewrite modules.
Creates a directory that specifies where to place the compressed package.
[Root@node1 ~]# mkdir-p/linuxidc/tools
[Root@node1 ~]# cd/linuxidc/tools/
[Root@node1 tools]# tar-zxf pcre-8.33.tar.gz
[Root@node1 pcre-8.33]#./configure
[Root@node1 pcre-8.33]# make && make install
[Root@node1 nginx-1.5.8]#./configure--with-http_stub_status_module--with-http_ssl_module--prefix=/data/nginx
[Root@node1 nginx-1.5.8]# make && make install

Note: The installation successfully starts Nginx, and you may encounter the following error.
[Root@node1 nginx-1.5.8]#/data/nginx/sbin/nginx-t
/data/nginx/sbin/nginx:error while loading shared libraries:libpcre.so.1:cannot open Shared object file:no such file o R Directory

Solution:
[Root@node1 ~]# cd/lib64/
[Root@node1 lib64]# ln-s libpcre.so.0.0.1 libpcre.so.1

3 This matter then start Nginx, view the next process and port.
[Root@node1 lib64]#/data/nginx/sbin/nginx-t
Nginx:the configuration file/data/nginx/conf/nginx.conf syntax is OK
Nginx:configuration file/data/nginx/conf/nginx.conf Test is successful
[Root@node1 lib64]#/data/nginx/sbin/nginx
[Root@node1 lib64]# Ps-ef | grep nginx
Root 8991 1 0 16:43? 00:00:00 Nginx:master Process/data/nginx/sbin/nginx
Nobody 8992 8991 0 16:43? 00:00:00 Nginx:worker Process
Root 8994 1907 0 16:44 pts/1 00:00:00 grep nginx
[Root@node1 lib64]# NETSTAT-ANPT | grep nginx
TCP 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8991/nginx

3. Configure the Nginx Web reverse proxy to achieve two Tomcat load balancing:
The Nginx configuration file is as follows:
[Root@node1 ~]# cat/data/nginx/conf/nginx.conf
User root;
Worker_processes 1;
#error_log Logs/error.log Info;
Pid/data/nginx/logs/nginx.pid;
Worker_rlimit_nofile 65535;
Events {
Use Epoll;
Worker_connections 65535;
Multi_accept on;
}

HTTP {
Include Mime.types;
Default_type Application/octet-stream;
CharSet Utf-8;
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 60;
Server_tokens off;
Limit_rate_after 3m;
Limit_rate 512k;
Tcp_nodelay on;
Client_header_buffer_size 256k;
Large_client_header_buffers 4 256k;

# Define Nginx Proxy Module
Proxy_http_version 1.1;
Proxy_connect_timeout 60;
Proxy_read_timeout 60;
Proxy_send_timeout 60;
Proxy_buffer_size 16k;
Proxy_buffers 4 64k;
Proxy_busy_buffers_size 128k;
Proxy_temp_file_write_size 128k;
Proxy_headers_hash_max_size 51200;
Proxy_headers_hash_bucket_size 6400;

# Define Gzip Compression module
gzip on;
Gzip_vary on;
Gzip_min_length 1k;
Gzip_buffers 4 16k;
Gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_types text/plain application/x-javascript text/css application/xml;

# Define Realserver Pool
Upstream linuxidc.com {
Ip_hash;
Server 192.168.1.15:8080 max_fails=0 weight=5;
Server 192.168.1.19:8080 max_fails=0 weight=5;
}

server {
Listen 80;
server_name linuxidc.net www.linuxidc.net;

Location/{
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Proxy_pass http://linuxidc.com;
Expires 1d;
Access_log Logs/host.access.log Main;
}

# Define 404 502 503 504 error page
Error_page 404/404.html;
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
}
}




Linux under load balancing between Apache and multiple tomcat clusters

Multiple computers default Tomcat ports are 8080

Exactly the same deployment under each Tomcat

On the basis of the preceding Apache-Tomcat integration

Modify Vi/etc/httpd/conf/httpd.conf

[Java]

LoadModule Jk_module modules/mod_jk-1.2.26-httpd-2.2.4.so
Jkworkersfile "Conf/workers.properties"
Jklogfile "Logs/mod_jk.log"
Jkloglevel Debug
Jkmount/*.jsp LoadBalancer

The worker.properties under Conf is modified as follows
[Java]

ps=/
Worker.list=worker1,worker2,loadbalancer
worker.worker1.port=8009
Worker.worker1.host=localhost #本机一台tomcat
Worker.worker1.type=ajp13
Worker.worker1.lbfactor=1
worker.worker2.port=8009
worker.worker2.host=10.228.56.80 #另外一台机器tomcat
Worker.worker2.type=ajp13
Worker.worker2.lbfactor=1
Worker.loadbalancer.type=lb
Worker.loadbalancer.balanced_workers=worker1, Worker2
# The load points to the machine

Configured successfully so that the load-balancing configuration is implemented




Nginx Tomcat Cluster load Balancing solution notes

1, Windows

Download Nginx Address: http://nginx.org/The version used in this article is 1.5.1

After the download after decompression, in the Conf directory modify nginx.conf My configuration as follows:


#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;

Client_header_timeout 3m;
Client_body_timeout 3m;
Send_timeout 3m;

Client_header_buffer_size 1k;
Large_client_header_buffers 4 4k;

Sendfile on;
Tcp_nopush on;
Tcp_nodelay on;
#tcp_nopush on;


#keepalive_timeout 0;
Keepalive_timeout 65;

#gzip on;
Upstream localhost {
#根据ip计算将请求分配各那个后端tomcat, many people mistakenly think that can solve the session problem, in fact, can not.
#同一机器在多网情况下, routing switches, IP may be different
#ip_hash;
Server localhost:9097 weight=1;
Server localhost:9098 weight=1;
Server localhost:9097 backup;
Server localhost:9098 backup;
#jvm_route $cookie _jsessionid|sessionid Reverse; #session复制的配置
Ip_hash;
}


Upstream Backend {
Server localhost:9097;
Server localhost:9098;
}

server {
Listen 9096;
server_name localhost;

#charset Koi8-r;

#access_log Logs/host.access.log Main;

Location ~ ^/(Web-inf)/{
Deny all;
}

# # #所以的静态文件人gif, JPG, etc. are open locally, storage directory for HTML, save time of 30 days
Location ~. *\. (gif|jpg|jpeg|png|bmp|swf|css|js|htm|html) $ {
root HTML;
Access_log off;
Expires 30d;
}
Location/{
root HTML;
Index index.jsp;
#include proxy.conf; #这个文件是我们新建的, to import
Proxy_pass http://localhost;
Proxy_redirect off; #default
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Client_max_body_size 10m;
Client_body_buffer_size 128k;
Proxy_connect_timeout 3; #90
Proxy_send_timeout 3; #90
Proxy_read_timeout 3; #90
Proxy_buffer_size 4k;
Proxy_buffers 4 32k;
Proxy_busy_buffers_size 64k;
Proxy_temp_file_write_size 64k;
}

#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$ {
# Proxy_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;
# server_name localhost;

# SSL on;
# ssl_certificate Cert.pem;
# Ssl_certificate_key Cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers high:!anull:! MD5;
# ssl_prefer_server_ciphers on;

# location/{
# root HTML;
# index index.html index.htm;
#    }
#}

}


Download Tomcat

Modify Port to 9097

Another modified to 9098

Start two Tomcat and Nginx separately in the address bar enter http://locahost/You can see the Tomcat welcome interface.

Linux and window configuration is basically the same, only the Linux under the configuration of Tomcat to be aware of the need to add environment variables

apache+tomcat Environment Setup (JK deployment process)

Apache is just a Web server and cannot run JSP programs. If you want to run the JSP program, you will also need to install Tomcat Server software to integrate Tomcat and Apche through the connector component. When a user requests access to the *.JSP Web page, the Tomcat service is automatically invoked by Aapache to parse and the resulting page is returned to the client browser.

System environment:

RHEL6.0, inner core version number 2.6.32-71.el6.x86_64

Required Software:

Httpd-2.2.15.tar.gz

Apache-tomcat-6.0.18.tar.gz

Jdk-6u25-linux-x64.bin

Mod_jk-1.2.28-httpd-2.2.x.so

Download MOD_JK Various versions, don't get down the wrong

http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/

Download JDK

Http://www.Oracle.com/technetwork/java/javase/downloads/jdk-6u25-download-346242.html

I. Installation of HTTPD service

[root@l-www.linuxidc.com ~] #tar XZVF httpd-2.2.15.tar.gz

[root@l-www.linuxidc.com ~] #cd httpd-2.2.15

[root@l-www.linuxidc.com ~]#./configure–prefix=/usr/local/apache2–enable-so–enable-rewrite && Make & & Make Install

Installation and configuration JDK development environment

1. Install JDK

[Root@l-www.linuxidc.com ~]# MV jdk-6u25-linux-x64.bin/usr/bin/

[Root@l-www.linuxidc.com ~]# cd/usr/local/

[root@l-www.linuxidc.com local]# chmod a+x jdk-6u25-linux-x64.bin

[root@l-www.linuxidc.com local]#./jdk-6u25-linux-x64.bin

The software license agreement will be displayed after the JDK Setup program, press Enter or space to the end, follow the prompts to enter "Yes" return confirmation, wait 1-2 minutes or so to complete the installation.

2, for the JDK installation directory and the implementation of the program to create linked files to facilitate the use.

[Root@l-www.linuxidc.com ~]# Ln-sf/usr/local/jdk1.6.0_25/usr/local/jdk6

[Root@l-www.linuxidc.com ~]# ln-sf/usr/local/jdk6/bin/java/usr/bin/

[Root@l-www.linuxidc.com ~]# ln-sf/usr/local/jdk6/bin/javac/usr/bin/

Modify the/etc/profile file to add the variable settings required by the Java development environment.

[Root@l-www.linuxidc.com ~]# Vi/etc/profile

Java_home=/usr/local/jdk6

Classpath=.: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar

Path= $JAVA _home/bin: $PATH

Export Java_home CLASSPATH PATH

[Root@l-www.linuxidc.com ~]# Source/etc/profile

3. Test Java

View the installed JDK program version information.

Java Version "1.6.0_25"

Java (TM) SE Runtime Environment (build 1.6.0_13-b03)

Java HotSpot (TM) Client VM (build 11.3-b02, mixed mode, sharing)

Write a Java test applet and compile the execution.

public class HelloWorld {

public static void Main (String args[]) {

System.out.println ("helloworld!!");

}

}

[root@l-www.linuxidc.com ~]# Javac Helloworld.java//Compiling program

[Root@l-www.linuxidc.com ~]# Java HelloWorld//Run Helloworld.class Program

helloworld!! Show Run Results

Third, install the configuration tomcat

1. Install Tomcat

The Tomcat installation process is very simple, as long as the source package is extracted to the destination folder.

[Root@l-www.linuxidc.com ~]# tar zxvf apache-tomcat-6.0.18.tar.gz

-c/usr/local/

2, configuration.

① creates link files for Tomcat's installation directory and for startup and shutdown scripts for easy use.

[Root@l-www.linuxidc.com ~]# Ln–sf/usr/local/apache-tomcat-6.0.18.tar.gz/usr/local/tomcat

[Root@l-www.linuxidc.com ~]# ln-sf/usr/local/tomcat/bin/startup.sh/usr/bin/tomcat-up

[Root@l-www.linuxidc.com ~]# Ln-sf/usr/local/tomcat/bin/shutdown.sh/usr/bin/tomcat-down

② Modify the/etc/profile file to set the variable catalina_home that the Tomcat service needs to use.

[root@l-www.linuxidc.com ~]# vi/etc/profile//Combined with the first step, confirm the following:

Java_home=/usr/local/jdk6

Classpath=.: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar: $CATALINA _home/lib/servlet-api.jar

Path= $JAVA _home/bin: $PATH

Export Java_home cactalina_home CLASSPATH PATH

[root@l-www.linuxidc.com ~]# source/etc/profile//Execute profile to make new configuration effective

3, modify the Tomcat-users.xml file, add the Administrator account "Tomcat" settings.

[Root@l-www.linuxidc.com ~]# Vi/usr/local/tomcat/conf/tomcat-users.xml

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>

<tomcat-users>

<user username= "Tomcat" password= "Tomcat" roles= "Manager,admin"/>

</tomcat-users>

(3) testing.

① starts the Tomcat service and confirms the listening status of the Tomcat service.

[Root@l-www.linuxidc.com ~]# tomcat-up

Using catalina_base:/usr/local/tomcat

Using Catalina_home:/usr/local/tomcat

Using Catalina_tmpdir:/usr/local/tomcat/temp

Using Jre_home:/usr/local/jdk6

[Root@l-www.linuxidc.com ~]# NETSTAT-ANPT | grep java

TCP 0 0::: 8009:::* LISTEN 7399/java

TCP 0 0::: 8080:::* LISTEN 7399/java

② access to http://172.16.12.40:8080, you can see Tomcat's web Home page (as shown in the following illustration)

③ You can manage Tomcat after you log on by clicking the Tomacat Manager link (as shown in the following illustration), if you want to stop the Tomcat service and perform "Tomcat-down".

4, the Tomcat service and the Aptech service to connect

Although Tomcat can run independently as a Web server, Tomcat's web functionality is far less powerful than Apache. In practical applications, Apache and Tomcat are integrated through the MOD_JDK Connector (connectors). The effort to invoke the Tomcat service is done by Apache, and the user only needs to access the Apache service.

(1) Install the MOD_JDK module.

[Root@l-www.linuxidc.com ~]# CP Mod_jk-1.2.28-httpd-2.2.x.so/usr/local/apache2/modules

(2) Adjust the Apache service, Tomcat service related configuration, and restart Tomcat, httpd services.

① Modify the http.conf configuration file to add the JDK call configuration.

[Root@l-www.linuxidc.com ~]# vi/usr/local/apache2/conf/httpd.conf

...

ServerName l-www.linuxidc.com

DocumentRoot "/usr/local/apache2/htdocs"

...

LoadModule Jk_module modules/mod_jk-1.2.28-httpd-2.2.x.so

Jkworkersfile conf/workers.properties

Jklogfile Logs/mod_jk.log

Jkshmfile LOGS/MOD_JK.SHM

Jkloglevel Notice

Jkmount/*/*.jsp Work1

Jkmountcopy All

AddType application/x-jsp. jsp

② Add Tomcat configuration file and create Web application directory Links

[Root@l-www.linuxidc.com ~]# Vi/usr/local/apache2/conf/workers.properties

Worker.list=work1

worker.work1.port=8009

Worker.work1.host=l-www.linuxidc.com

Worker.work1.type=ajp13

Worker.work1.lbfactor=1

[Root@l-www.linuxidc.com ~]# Cd/usr/local/tomcat

[root@l-www.linuxidc.com tomcat]# MV WebApps Webapps.bak

[Root@l-www.linuxidc.com tomcat]# Ln-sf/usr/local/apache2/htdocs WebApps

③ restart Apache and Tomcat services.

[Root@l-www.linuxidc.com ~]# Tomcat-down; Tomcat-up

[Root@l-www.linuxidc.com ~]#/usr/local/apache2/bin/apachectl Restart

(3) test

① Create a JSP test page that displays the system time.

[Root@l-www.linuxidc.com ~]# mkdir/usr/local/apache2/htdocs/apps/

[Root@l-www.linuxidc.com ~]# vi/usr/local/apache2/htdocs/apps/showtime.jsp

<% @page language= "java" import= "java.util.*"%>

Now,the Time&date is: <%out.println (new Date ());%>

②http://192.168.0.10/apps/showtime.jsp, the current system time should be displayed in the Web page

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.