标签:lnmp
实验环境:
hostname ip
nginx 192.168.1.241
java1 192.168.1.242
java2 192.168.1.243
1.nginx+upstream安装
[root@nginx ~]# useradd nginx -s /sbin/nologin
[root@nginx opt]# tar zxf pcre-8.34.tar.gz
[root@nginx opt]# cd pcre-8.34
[root@nginx pcre-8.34]# ./configure
[root@nginx pcre-8.34]# make && make install
[root@nginx opt]# tar zxf tengine-1.5.1.tar.gz
[root@nginx opt]# cd tengine-1.5.1
[root@nginx tengine-1.5.1]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@nginx tengine-1.5.1]# make && make install
(1)修改配置文件
[root@nginx ~]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@nginx ~]# vi /usr/local/nginx/conf/nginx.conf
添加以下配置
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安装
http://304076020.blog.51cto.com/7503470/1544311
安装过程请参照上面网址
3.tomcat安装 (安装 java1和java2两台配置一样)
http://304076020.blog.51cto.com/7503470/1642482
安装过程请参照上面网址
(1)将以下jar包放到/usr/local/tomcat/lib下
jar包下载地址
http://down.51cto.com/data/2044436
(2)修改配置文件
[root@java1 ~]# vi /usr/local/tomcat/conf/context.xml
在 </Context> 上一行添加以下配置
<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"
/>
如图
[root@java2 ~]# vi /usr/local/tomcat/conf/context.xml
在 </Context> 上一行添加以下配置
<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"
/>
如图
3)新建测试页面
[root@java1 ~]# vi /web/data/test.jsp
添加以下配置
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server java1!");
%>
[root@java2 ~]# vi /web/data/test.jsp
添加以下配置
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server java2!");
%>
(4)启动tomcat
[root@java1 ~]# /usr/local/tomcat/bin/startup.sh
[root@java2 ~]# /usr/local/tomcat/bin/startup.sh
测试访问tomcat
(5)启动nginx
[root@nginx ~]# /usr/local/nginx/sbin/nginx -s start
测试访问nginx
由上图可见,nginx现在负载在tomcat2上,关闭tomcat2,访问测试session id会不会变。
[root@java2 ~]# /usr/local/tomcat/bin/shutdown.sh
由上图可见,nginx现在负载在tomcat1上,此时session id一直没变,表示共享成功。
本文出自 “Linux艺术(Q群:1991706)” 博客,请务必保留此出处http://304076020.blog.51cto.com/7503470/1643526
Nginx+Tomcat+Memcached实现tomcat集群和session共享
标签:lnmp
原文地址:http://304076020.blog.51cto.com/7503470/1643526