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, 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

2, Start Redis

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

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

2,tomcat Preparing for Work 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, 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

3. Modify the Context.xml configuration of Tomcat

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

  1. <!--using Redis for session sharing--

  2. <Valve classname="com.radiadesign.catalina.session.RedisSessionHandlerValve" />

  3. <Manager classname="Com.radiadesign.catalina.session.RedisSessionManager"

  4. host="127.0.0.1"

  5. port="6379"

  6. password="Root"

  7. database="0"

  8. maxinactiveinterval="3600"/>

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

650) this.width=650; "Src=" http://img.blog.csdn.net/20141221162759241?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvc29pbg92ztc=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Center "style=" border:0px; "/>

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 1, annotations default access

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

Listen 80;

2, add the application server that needs the 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:

    1. Upstream pool{

    2. Server 127.0.0.1:8080;

    3. Server 127.0.0.1:8081;

    4. Server 127.0.0.1:8082;

    5. }

3, configure access domain name

Continue to add in test.conf:

  1. server {

  2. Listen default_server;

  3. server_name www.test.cn;

  4. gzip on;

  5. Gzip_min_length 1k;

  6. Gzip_buffers 4 16k;

  7. Gzip_http_version 1.0;

  8. Gzip_comp_level 2;

  9. Gzip_types text/plain application/x-javascript text/css application/xml;

  10. Gzip_vary on;

  11. Location/{

  12. Proxy_set_header Host $host;

  13. Proxy_set_header x-forwarded-for $remote _addr;

  14. Proxy_connect_timeout 3;

  15. Proxy_send_timeout 30;

  16. Proxy_read_timeout 30;

  17. Proxy_pass Http://pool;

  18. }

  19. Error_page 502 503 504/50x.html;

  20. Location =/50x.html{

  21. Root/home/workspace/server/apache-tomcat-6.0.41-8080/webapps/test;

  22. }

  23. if ($host! = ' www.test.cn ') {

  24. Rewrite ^/(. *) $ http://www.test.cn/$1 permanent;

  25. }

  26. }

4. Configure static resource access
    1. server {

    2. Listen 80;

    3. server_name static.test.cn;

    4. location/static {

    5. Root/home/workspace/server/apache-tomcat-6.0.41-8080/webapps/test;

    6. }

    7. Location/media {

    8. Root/home/workspace/file;

    9. }

    10. }

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, 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.