Nginx + memcached + Tomcat cluster configuration

Source: Internet
Author: User
1. nginx

Nginx binds multiple Web servers to the same IP address to achieve load balancing among multiple web servers and reduce the load of a single web server to improve overall performance and stability.

The following is a simple method for installing and configuring nginx:

1) download and decompress nginx to the local directory: http://nginx.org/en/download.html

2) nginx configuration is mainly to modify its configuration file/CONF/nginx. conf. The following link describes the attributes of the nginx. conf configuration file:

Http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html

To use nginx to configure Server Load balancer for Web servers, you need to modify the following:

A) configure the upstream attribute under the HTTP tag, for example:

 

[Plain]View plaincopy
  1. Upstream mysvr {
  2. # The weigth parameter indicates the weight. A higher weight indicates a higher probability of being assigned.
  3. Server 192.168.8.1x: 3128 Weight = 5;
  4. Server 192.168.8.2x: 80 Weight = 1;
  5. Server 192.168.8.3x: 80 Weight = 6;
  6. }

 

Weight is optional. If weight is not configured, all Web servers have the same weight by default.

B) modify the server label under the HTTP label, for example:

 

[Plain]View plaincopy
  1. Server {
  2. Listen 83;
  3. SERVER_NAME localhost;
  4. Location /{
  5. Root/root;
  6. Index index. jsp;
  7. Proxy_pass http: // mysvr;
  8. Proxy_set_header host $ HOST: 83;
  9. Proxy_set_header X-real-IP $ remote_addr;
  10. Proxy_set_header X-forwarded-for $ proxy_add_x_forwarded_for;
  11. }
  12. ......
  13. }

 

It should be noted that SERVER_NAME and listen respectively set the user access address and port; proxy_pass specifies the list of servers to be switched (defined in upstream ); proxy_set_header host sets the address in the HTTP packet to be redirected to the address specified in proxy_pass. You can also specify the port number here. If this parameter is not specified, port 80 is redirected by default.

For other attributes that need to be set, see the details in the above link.

3) after the configuration is complete, enter the nginx root directory in the console. Run nginx or nginx-s start to start the nginx server, run nginx-s stop or nginx-s quit to disable nginx (do not use Ctrl + C to force the end ).

2. memcached

Memcached is a key-value ing cache system. In webapplication, memcached can be used to cache frequently accessed objects. In distributed Web Server Clusters, memcached must be used to support the consistency of sessions in individual web servers.

Here we need to use an open-source project, namely memcached Session Manager (MSM). The following link is the official website of this open-source project:

Http://code.google.com/p/memcached-session-manager/

The following link is the official installation and Configuration Wizard:

Http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration

The following is a simple method to use MSM to manage the unified session storage of Tomcat server groups in clusters through nginx:

1) download, install, and run memcached:

Linux: http://memcached.org/

Http://code.jellycan.com/memcached/ for Windows

2) download the required jar package and place it in the Lib path of Tomcat:

A) memcached-session-Manager-$ {version}. jar, Basic Package

B) memcached-session-manager-tc6-$ {version}. Jar (for Tomcat 6) or memcached-session-manager-tc7-$ {version}. Jar (for Tomcat 7), server support package

C) spymemcached-2.8.12.jar and couchbase-client-1.1.4.jar, cache support package

D) msm-javolution-serializer.jar and javolution-5.4.3.1.jar, serialization support package

3) In the/CONF/context. xml file of Tomcat, add the following code under the root tag <context>:

 

[HTML]View plaincopy
  1. <Managerclassnamemanagerclassname = "De. javakaffee. Web. MSM. memcachedbackupsessionmanager"
  2. Memcachednodes = "$ {tag}: localhost: 11211"
  3. Requesturiignorepattern = ". */. (PNG | GIF | JPG | CSS | JS) $"
  4. Sessionbackupasync = "false"
  5. Sessionbackuptimeout = "100"
  6. Transcoderfactoryclass = "De. javakaffee. Web. MSM. serializer. javolution. javolutiontranscoderfactory"
  7. Copycollectionsforserialization = "false"/>

 

$ {Tag} can be replaced with any string to mark the webserver.

4) after completing the preceding configuration, start Tomcat. If no error occurs, the configuration is successful. When the session ID is obtained through the GETID method of httpsession, the above mark is displayed at the end of the ID.

 

Reprinted: http://blog.csdn.net/sadfishsc/article/details/9151083

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.