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

Source: Internet
Author: User

Nginx-tomcatLoad Balancingredis-sessionshared, static resource separation

Basic Environment:

redis-2.8

apache-tomcat-6.0.41

nginx1.6.2

1,RedisConfiguration1, configureRedisAccess 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

2, startRedis

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

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

2,Tomcatpreparatory work1, prepareTomcatApplication Server

Deploy an app to Tomcatthat 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, addingTomcatand theRedisdoSessionshared byJarPackage

add the jar packages required for session sharing under Tomcat 's own Lib , and be aware The version of Tomcat corresponds.

Commons-pool-1.6.jar

Jedis-2.1.0.jar

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

3, modifyTomcatof thecontext.xmlConfiguration

Locate the context.xml configuration file in the conf directory of Tomcat , and in the label <context > Add:

<!--use Redis for session sharing--><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"/>


The configuration provided by default does not have the password attribute, we can view the source code of Tomcat-redis-session-manager-1.2-tomcat-6.jar, can be in the password attribute found in Redissessionmanager.class .

4, copyTomcatModify IndividualTomcatthe Port

After configuring the Tomcat and redis shared configuration, we can copy this tomcat to multiple copies and then modify Tomcat ports, such as:

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,NginxConfiguration1, comment default Access

Under the nginx/conf.d/ of Ngix , there is a default.confthat will be inside the listen 80 Default_server instead :

Listen 80;

2To add an application server that requires load

Create a confin 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, configure the access domain name

Continue to add in test.conf :

server {    listen 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   3;      Proxy_send_timeout      ;      Proxy_read_timeout      ;      Proxy_pass http://pool;  }  Error_page 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;  }}


4, configure static resource access

server {     listen;     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;     }}


5, modify 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, startNginxand theTomcat, Test

Test Load Balancing:

Start nginx and all Tomcat, then log in to the Web page, and then start another Tomcatandclose 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 Tomcatandclose 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.