Centos6.2 for Nginx and Tomcat integration and load balancing (already practiced)

Source: Internet
Author: User

Centos6.2 do the Nginx and Tomcat the integration and load balancing

---------------------------------------------------------JDK------------------------------------------------------------- ------

1 : CentOS Install JDK6 on

JDK:

Jdk-6u34-linux-i586.bin

installation command:

./jdk-1_5_0_06-linux-i586.bin

The configuration environment variables are as follows :

Open/etc/profile with VI add the following at the end of the file

Java_home= "/USR/LOCAL/JDK"

Class_path= "$JAVA _home/lib:java_home/jre/lib"

Path= ".: $PATH:%java_home/bin%"

Export Java_home Catalina_home

Test:

Test whether the relevant Java information will appear through the java–version command, and the installation is successful.

---------------------------------------------------------JDK------------------------------------------------------------- ------

---------------------------------------------------------Nginx----------------------------------------------------------- -----

2 : Installing Nginx

Nginx:

Download: http://nginx.org/en/download.html

installation command:

Step 1:RPM-IVH nginx-release-centos-6-0.el6.ngx.noarch.rpm

Step 2:yum Install Nginx

Will show a lot of information, ask you OK not OK AH: Is this OK [y/n]:
Enter Y, the screen rolls for a while and then installs, the last hint "complete!" Is that it's finished.

Software Description:
1 configuration directory:/etc/nginx/
2 pid directory:/var/run/nginx.pid
3 Error log:/var/log/nginx/error.log

Nginx Common commands:

1 Start: Nginx
2 Restart: killall-hup nginx
3 Test configuration: Nginx–t

Test :

Using the command "Nginx" Start service, enter "127.0.0.1" in the browser if a welcome message appears stating that the installation was successful

---------------------------------------------------------Nginx----------------------------------------------------------- -----

---------------------------------------------------------Tomcat---------------------------------------------------------- ----

3 : Installing Tomcat

Tomcat:

Apache-tomcat-6.0.35.tar.gz

installation command:

Tar xvzf apache-tomcat-6.0.35.tar.gz (extracted to the current directory)

MV Tomcat (source)/usr/local/tomcat---move to target directory

Common commands:

Start: bin/startup.sh

Close: bin/shutdown.sh

Test:

127.0.0.1:8080 whether the Tomcat information page appears

---------------------------------------------------------Tomcat---------------------------------------------------------- ----

---------------------------------------------------------Integration-------------------------------------------------------------- ---

4 : Integrated Nginx and Tomcat

Add the following content in /etc/nginx/nginx.conf http {}

Upstream www.imeixi.cn {

Server 127.0.0.1:8080 weight=1; #转向tomcat

}

in/etc/nginx/conf.d/default.conf modified in

server{

Listen 80;

server_name localhost; It could be www.imeixi.cn.

Location/{

root HTML;

Index index.jsp index.htmlindex.htm;

Proxy_redirect off;

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://www.mygogo.com    ; This is referred to as forwarding (Nginx is configured as a reverse proxy)

}

Error_page 502 503 504/50x.html;

Location =/50x.html {

root HTML;

}

}

Need to restart Nginx to take effect after adding

Killall-hup Nginx

Test:

Add a simple web app to tomcat (an HTML page is OK) and Access 127.0.0.1/yourweb (nginx-Figure 1) and 127.0.0.1:8080/yourweb (Tomcat Figure 2) to see if the same page appears. There is success

Figure 1

Figure 2

---------------------------------------------------------Integration-------------------------------------------------------------- ---

---------------------------------------------------------Load Balancing----------------------------------------------------------

5 : Load Balancing

1. Frame Composition:

2 , install a second tomcat:

Since the installation of two Tomcat on the same machine, the corresponding changes should be made as follows

1: Generate a new tomcat from the first Tomcat

Cp/usr/local/tomcat/usr/local/tomcat2

2: Add the following at the end of the/etc/profile file "can be omitted"

Catalina_2_base=/usr/local/tomcat2

Catalina_2_home=/usr/local/tomcat2

Tomcat_2_home=/usr/local/tomcat2

Export Catalina_2_base catalina_2_home tomcat_2_home

3: At the top of the tomcat2/bin/catalina.sh file , add the following "can be omitted"

Export JAVA_HOME=/USR/LOCAL/JDK

Export path= $PATH: $JAVA _home/bin

Export Classpath= $JAVA _home/lib

Export Catalina_home= $CATALINA _2_home

Export catalina_base= $CATALINA _2_base

Change Profile After you use the #source/etc/profile make the newly configured environment variable effective

4: Modify the Tomcat2/conf/server.xml file

Modify three listening ports

<server port= "8005" shutdown= "shutdown" > Changed to <server port= "9005" shutdown= "Shutdown" >

<connector port= "8080" protocol= "http/1.1"

connectiontimeout= "20000"

redirectport= "8443"/> Changed to <connector port= "9080" protocol= "http/1.1"

connectiontimeout= "20000"

redirectport= "8443"/>

<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/> Changed to <connector port= "9009" protocol= "AJP/1.3" redirectport= "8443"/>

5: Publish the same web app under two Tomcat and start two Tomcat

/usr/local/tomcat/bin/startup.sh

/usr/local/tomcat2/bin/startup.sh

3 , configure Nginx

in/etc/nginx/nginx.conf Add the following HTTP {} in Add

Upstream www.imeixi.cn {

Server 127.0.0.1:8080 weight=1; Server localhost:8080 weight=1 (weight)

Server 127.0.0.1:9080 weight=1; Server localhost:9080 weight=5

}

in/etc/nginx/conf.d/default.conf changes are not made.

server {

Listen 80;

server_name www.imeixi.cn;

Location/{

root HTML;

Index index.jsp index.htmlindex.htm;

Proxy_redirect off;

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://www.mygogo.com;

}

Error_page 502 503 504/50x.html;

Location =/50x.html {

root HTML;

}

}

Need to restart Nginx to take effect after adding

Killall-hup Nginx

4 , testing:

Access to www.imeixi.cn will be a round-trip access to two Tomcat

---------------------------------------------------------Load Balancing----------------------------------------------------------

Centos6.2 for Nginx and Tomcat integration and load balancing (already practiced)

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.