Nginx+tomcat+memcached for Tomcat cluster and session sharing

Source: Internet
Author: User
Tags session id tomcat server

Lab Environment:

Hostname IP

Nginx 192.168.1.241

JAVA1 192.168.1.242

JAVA2 192.168.1.243


1.nginx+upstream Installation

[Email protected] ~]# useradd nginx-s/sbin/nologin

[Email protected] opt]# tar zxf pcre-8.34.tar.gz

[Email protected] opt]# CD pcre-8.34

[Email protected] pcre-8.34]#./configure

[[email protected] pcre-8.34]# make && make install

[Email protected] opt]# tar zxf tengine-1.5.1.tar.gz

[Email protected] opt]# CD tengine-1.5.1

[Email protected] tengine-1.5.1]#/configure--user=nginx--group=nginx--prefix=/usr/local/nginx--with-http_stub_ Status_module--with-http_ssl_module

[[email protected] tengine-1.5.1]# make && make install

(1) Modify the configuration file

[Email protected] ~]# Mv/usr/local/nginx/conf/nginx.conf/usr/local/nginx/conf/nginx.conf.bak

[Email protected] ~]# vi/usr/local/nginx/conf/nginx.conf

Add the following configuration

User Nginx Nginx;

Worker_processes Auto;

Worker_rlimit_nofile 65535;

Error_log/var/log/www/error.log;

#pid Logs/nginx.pid;

Events {

Use Epoll;

Worker_connections 51200;

}

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";


Server_info off;

Server_tag off;

Server_name_in_redirect off;

Access_log/var/log/www/access.log main;

Client_max_body_size 20m;

Client_header_buffer_size 16k;

Large_client_header_buffers 4 16k;

Sendfile on;

Tcp_nopush on;

Keepalive_timeout 65;

Server_tokens on;

gzip on;

Gzip_min_length 1k;

Gzip_buffers 4 16k;

Gzip_proxied any;

Gzip_http_version 1.1;

Gzip_comp_level 3;

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

Gzip_vary on;

Upstream Kim {

Consistent_hash $request _uri;

#ip_hash;

Server 192.168.1.242:8080;

Server 192.168.1.243:8080;

}

server {

Listen 80;

server_name localhost;

Location/{

root HTML;

Index index.html index.htm;

Proxy_pass Http://kim;

Proxy_redirect off;

Proxy_set_header Host $host;

Proxy_set_header X-real-ip $remote _addr;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

Client_max_body_size 10m;

Client_body_buffer_size 128k;

Proxy_connect_timeout 90;

Proxy_send_timeout 90;

Proxy_read_timeout 90;

Proxy_buffer_size 4k;

Proxy_buffers 4 32k;

Proxy_busy_buffers_size 64k;

Proxy_temp_file_write_size 64k;

}

}

}


2.memcached Installation

http://304076020.blog.51cto.com/7503470/1544311

Please refer to the above URL for the installation process

3.tomcat installation (same as JAVA1 and java2 two configurations)

http://304076020.blog.51cto.com/7503470/1642482

Please refer to the above URL for the installation process

(1) Place the following jar packages under/usr/local/tomcat/lib

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/75/wKioL1VJ3s6RqW7DAAIBLR85Tak227.jpg "title=" 1.png " alt= "Wkiol1vj3s6rqw7daaiblr85tak227.jpg"/>


Jar Package

http://down.51cto.com/data/2044436

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/79/wKiom1VJ3aiQHkMkAAS9Vk2IgZI383.jpg "title=" 2.png " alt= "Wkiom1vj3aiqhkmkaas9vk2igzi383.jpg"/>

(2) Modify the configuration file

[Email protected] ~]# Vi/usr/local/tomcat/conf/context.xml

Add the following configuration on the </Context> line

<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"

Memcachednodes= "n1:127.0.0.1:11211"

Sticky= "false"

Sessionbackupasync= "false"

Lockingmode= "Auto"

Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "

Transcoderfactoryclass= "De.javakaffee.web.msm.JavaSerializationTranscoderFactory"

/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/79/wKiom1VJ3pqRfcJzAAEROcS1zlY187.jpg "title=" 3.png " alt= "Wkiom1vj3pqrfcjzaaerocs1zly187.jpg"/>

[Email protected] ~]# Vi/usr/local/tomcat/conf/context.xml

Add the following configuration on the </Context> line

<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"

Memcachednodes= "n1:192.168.1.242:11211"

Sticky= "false"

Sessionbackupasync= "false"

Lockingmode= "Auto"

Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "

Transcoderfactoryclass= "De.javakaffee.web.msm.JavaSerializationTranscoderFactory"

/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/79/wKiom1VJ3rXQ2xuuAAE-hvVvsrs755.jpg "title=" 4.png " alt= "Wkiom1vj3rxq2xuuaae-hvvvsrs755.jpg"/>

3) New test page

[Email protected] ~]# vi/web/data/test.jsp

Add the following configuration

Sessionid:<%=session.getid ()%>

<BR>

Sessionip:<%=request.getservername ()%>

<BR>

Sessionport:<%=request.getserverport ()%>

<%

Out.println ("This is Tomcat Server java1!");

%>


[Email protected] ~]# vi/web/data/test.jsp

Add the following configuration

Sessionid:<%=session.getid ()%>

<BR>

Sessionip:<%=request.getservername ()%>

<BR>

Sessionport:<%=request.getserverport ()%>

<%

Out.println ("This is Tomcat Server java2!");

%>

(4) Start Tomcat

[Email protected] ~]#/usr/local/tomcat/bin/startup.sh

[Email protected] ~]#/usr/local/tomcat/bin/startup.sh

Test access Tomcat

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/75/wKioL1VJ4Fjz2DTGAAD6bM2fd6A589.jpg "style=" float: none; "title=" 5.png "alt=" Wkiol1vj4fjz2dtgaad6bm2fd6a589.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/79/wKiom1VJ3ufhsW2QAAETYKTPiyU008.jpg "style=" float: none; "title=" 6.png "alt=" Wkiom1vj3ufhsw2qaaetyktpiyu008.jpg "/>

(5) Start Nginx

[[email protected] ~]#/usr/local/nginx/sbin/nginx-s start

Test Access Nginx

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/79/wKiom1VJ3yHRiWQmAAEYh2BdrpU517.jpg "title=" 7.png " alt= "Wkiom1vj3yhriwqmaaeyh2bdrpu517.jpg"/>

By visible, Nginx now load on TOMCAT2, close TOMCAT2, Access test session ID will not change.

[Email protected] ~]#/usr/local/tomcat/bin/shutdown.sh

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/7A/wKiom1VJ30DBs1fkAAETPmW4t64785.jpg "title=" 8.png " alt= "Wkiom1vj30dbs1fkaaetpmw4t64785.jpg"/>


By visible, Nginx now load on TOMCAT1, at this time the session ID has not changed, indicating that the sharing is successful.














This article is from the "Linux art (q Group: 1991706)" blog, please be sure to keep this source http://304076020.blog.51cto.com/7503470/1643526

Nginx+tomcat+memcached for Tomcat cluster and session sharing

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.