Nginx-tomcat load balancing redis-session sharing, static resource separation

Source: Internet
Author: User

Basic Environment:

redis-2.8

apache-tomcat-6.0.41

nginx1.6.2

1.redis configuration 1.1 Configuring the Redis access password

Find the redis.conf in the Redis directory and unpack the Requirepass comment (this property is used to set the password).

Such as:

Requirepass Root

1.2 Starting Redis

Start Redis in a way that Redis's directory is running

Redis-server/root/redis-2.8.9/redis.conf &


2,tomcat ready to work 2.1 preparing the Tomcat application server

Deploy an app to Tomcat that will contain the files that need to be statically processed, such as IMG, stored separately under a folder, such as static. Store user-generated media files in a common directory, such as/data/media. After the use of Nginx configuration processing.

2.2 Add Tomcat and Redis to do session shared jar package

Adding the jar packages required for session sharing under Tomcat's own LIB requires attention to the version of Tomcat.

Commons-pool-1.6.jar

Jedis-2.1.0.jar

Tomcat-redis-session-manager-1.2-tomcat-6.jar

2.3 Modifying the context.xml configuration of Tomcat

Locate the Context.xml profile in the Conf directory of Tomcat and add it under tags <Context>:

 <!--  redis  sharing with session   -->      <valve classname= "Com.radiadesign.catalina.session.RedisSessionHandlerValve"  / >     <manager classname= " Com.radiadesign.catalina.session.RedisSessionManager "     host=" 127.0.0.1 "      port= "6379"      password= "root"       database= "0"      maxinactiveinterval= "3600"/> 

&NBSP;&NBSP;

The provided configuration does not have the Password property by default. We can view the Tomcat-redis-session-manager-1.2-tomcat-6.jar source code, the password attribute that can be found in Redissessionmanager.class. get "" SPRINGMVC integrated mybatis Framework source code Bootstrap

2.4 Copy Tomcat to modify the ports for each tomcat

After configuring the Tomcat and Redis shared configuration, we can copy this tomcat to multiple copies and then modify the Tomcat port, for example:

Tomcat1:server port= "8005", Connector port= "8080", AJP Connector port= "8009"

Tomcat2:server port= "8006", Connector port= "8081", AJP Connector port= "8010"

Tomcat3:server port= "8007", Connector port= "8082", AJP Connector port= "8011"

3 Nginx Configuration 3.1 Comment default Access

In the Ngix nginx/conf.d/there is a default.conf, will be inside the listen default_server to:

Listen 80;

3.2 Adding an application server that requires load

Create a conf in the nginx/conf.d/of Ngix, such as test.conf. Add the upstream pool configuration section inside, and fill in the following addresses for each server that needs to be loaded:

Upstream pool{server 127.0.0.1:8080;        Server 127.0.0.1:8081;   Server 127.0.0.1:8082; }
3.3 Configuring Access domain names

Continue to add in test.conf:

&NBSP;

server {      listen 80 default_server;       server_name www.test.cn;      gzip 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;      gzip_ vary on;       location / {         proxy_set_header Host  $host;         proxy_ set_header x-forwarded-for  $remote _addr;        proxy_ Connect_timeout  &nBsp;3;        proxy_send_timeout      30 ;        proxy_read_timeout      30;         proxy_pass http://pool;    }     error_page 500 502 503 504 /50x.html;     location = /50x.html{        root /home/workspace/ server/apache-tomcat-6.0.41-8080/webapps/test;    }    if  ($host  !=  ' www.test.cn '  ) {      rewrite ^/(. *) $ http:// www.test.cn/$1 permanent;    }     }
3.4 Configuring Static resource access
server {Listen 80;          server_name static.test.cn;       location/static {root/home/workspace/server/apache-tomcat-6.0.41-8080/webapps/test;       } location/media {root/home/workspace/file; }   }


3.5 Modifying the size of the request body

Adding the Client_max_body_size property to http {}, this property affects a lot of places, such as uploading.

Client_max_body_size 100m;

4 start Nginx and Tomcat, test

Test Load Balancing:

Start Nginx and all Tomcat, then log in to the Web page, and then start another tomcat and close the previous one. See if you can still access it. Turn on all Tomcat tools to launch a large number of requests to see if the request was distributed to each tomcat.

Test session Sharing:

Start Nginx and a tomcat, then log in to the Web page, then start another tomcat and close the previous one. Check to see if you are still logged in.


Nginx-tomcat load balancing redis-session sharing, static resource separation

Related Article

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.