Nginx + Memcached + Tomcat cluster configuration

Source: Internet
Author: User
: This article mainly introduces Nginx + Memcached + Tomcat cluster configuration. For more information about PHP tutorials, see. This operation is mainly demonstrated on win7 64.

    • Web application construction
    • Install and configure Memcached to start
    • Tomcat configuration
      • Jar package
        • Memcached-session-manager
        • Serialization
      • Contextxml configuration
    • Start Nginx installation configuration
    • Test
    • Reference

Web application construction

The simple construction of web applications is not described here. For details, visit my Github.
For ease of display, make a difference in success. jsp, one plus 8080, one plus 8081.

Install, configure, and start Memcached

In this example, two memcached nodes are used on one machine.

On win7 64. Resources can be downloaded from this link.
Decompress the package and go to the cmd Directory. remember to run the package as an administrator.

Install

memcached  -d install

Run

memcached -d start

You can also start multiple memcached services. refer to this link.

Create

sc create"Memcached Server"start= auto binPath= "memcached_path -d runservice -m 32 -p 11311 -l 127.0.0.1" DisplayName= "Memcached Server"

Note: modify the port. the default value is 11211.
Access via Telnet: telnet 127.0.0.1 11211
Telnet interface quit can exit

Run the following command to start, stop, and uninstall Memcached Server:

SC start "Memcached Server" // start SC stop "Memcached Server" // stop SC delete "Memcached Server" // uninstall this service

You can see the memcached service in the task manager.

Tomcat configuration

This example uses two Tomcat 7.0.62 instances.

Tomcat cluster configuration. each node in the cluster shares sessions stored in the cached Memcached through sharing: If the Tomcat service on one machine is stopped, session Data on other peer servers can still be read from the Memcached cache without session loss.

Use memcached-session-manager, an official document.

Tomcat configuration mainly includes two aspects. First, you need to download the required package and put it in the lib directory under the tomcat installation directory (strictly speaking, it should be $ CATALINA_HOME/lib /)
Jar required for serialization of the first msm,
Second configuration context. xml

Jar package

Memcached-session-manager

Maven configuration for tomcat 7
The following three jar packages are required;
Memcached-session-manager-version.jar,
Memcached-session-manager-tc7-version.jar,
Spymemcached-version.jar

You can download it in maven below


  
   
    de.javakaffee.msm
   
   
    memcached-session-manager-tc7
   
   
    1.8.3
   
  
Serialization

Four types of serialization

kryo-serializer:
  
   
    de.javakaffee.msm
   
   
    msm-kryo-serializer
   
   
    version
   
  javolution:
  
   
    de.javakaffee.msm
   
   
    msm-javolution-serializer
   
   
    version
   
  xstream:
  
   
    de.javakaffee.msm
   
   
    msm-xstream-serializer
   
   
    version
   
  flexjson:
  
   
    de.javakaffee.msm
   
   
    msm-flexjson-serializer
   
   
    version
   
  

The jar packages required for serialization are as follows:
-Kryo-serializer: msm-kryo-serializer, kryo-serializers, kryo, minlog, reflectasm, asm
-Javolution-serializer: msm-javolution-serializer, javolution
-Xstream-serializer: msm-xstream-serializer, xstream, xmlpull, xpp3_min
-Flexjson-serializer: msm-flexjson-serializer, flexjson

$ CATALINA_HOME/lib/add jar as follows

Context. xml configuration

Modify the content under the context node in the $ CATALINA_HOME/conf/Context. xml file and submit the MSM configuration.
Memcached is mainly used to manage sticky sessions and non-sticky sessions, and membase is used to manage non-sticky sessions.
The example is based on two memcached instances on a host. the serialization method used in this example is kryo.

This operation uses sticky sessions for management. for non-sticky sessions, you can configure them by yourself or add them again next time. the configuration is as follows. The two tomcat configurations are the same.


  
       ...
   
    "false"        lockingMode="auto"        requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
   
    

Install, configure, and start Nginx

Download stable versions from the official website,

Decompress the package and enter the directory,

Start nginx // start

In this way, the nginx service is started. Open the Task Manager and check the nginx.exe process. two processes are displayed, occupying system resources, which is quite small. Then open the browser and enter http: // 127.0.0.1/to view the nginx welcome page, which is very friendly.

Nginx-s stop // stop nginxnginx-s reload // reload the configuration file nginx-s quit // exit nginx

By default, Nginx uses the default configuration file nginx. conf and modifies it as follows:

#user  nobody;worker_processes2;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections1024;}http {    include       mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status$body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';    access_log  logs/access.log  main;    sendfileon;    #tcp_nopush     on;#keepalive_timeout  0;#keepalive_timeout  65;#gzip  on;upstream localhost {          #ip_hash;server localhost:8080 weight=1;          server localhost:8081 weight=1;    }    server {        listen80;        server_name  localhost;        charset utf-8;        #charset koi8-r;#access_log  logs/host.access.log  main;location / {            root   html;            index  index.html index.htm;            proxy_passhttp://localhost;              proxy_connect_timeout2s;            proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        }        location~ ^/(WEB-INF)/ {           deny all;           }           #error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page500502503504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}    }    # another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

Test

Login Page

Success page
Success1

Success2

When tomcat (8080) is disabled, call tomcat (8081) to display success2;
When the memcached service is disabled, it is displayed as normal.

Reference

Nginx + Memcached + Tomcat cluster configuration practice (Sticky Session)
Start multiple Memcached services on the same Windows machine
Introduction and use of MSM-Memcached_Session_Manager
Memcached-session-manager configuration
Install and configure Nginx in Windows

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the Nginx + Memcached + Tomcat cluster configuration, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.