Nginx+tomcat
的
session
会话同步(2)
二
Nginx
的安装(在安装
nginx
前需要安装
prce
)
首先安装
pcre
tar zxvf pcre-7.8.tar.gz
cd zxvf pcre-7.8
./configure
make
make install
默认路径是
/usr/local
然后安装
nginx
tar zxvf nginx- 0.8.17 .tar.gz
cd nginx- 0.8.17
./configure --prefix=/usr/local/nginx
make
make install
三负载均衡的实现以及
session
会话的同步。
在这里我们要注意三个配置文件,两个是
tomcat
的
server.xml
(主配置文件)和
web.xml
,默认路径在:
/usr/local/apache-tomcat- 6.0.29 /conf
下面。另一个是
nginx
的配置文件
nginx.conf
,默认路径在:
/usr/local/nginx/conf
下面。另外还得注意
/usr/local/apache-tomcat-6.0.29/webapps
里面建一个文件夹
aa(
看你个人爱好
)
再在
aa
中写一个
index.jsp
的测试页(两台机器的
webapps
都要这么做哦)
现在我们要做的就是修改配置文件了
①
修改
server.xml
在两台服务器的tomcat的配置文件中分别找到:
<En
gine name="Catalina" defaultHost="localhost" >
分别修改为:
Tomcat01:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
Tomcat02:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
再在其后加上以下内容
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.2"
bind="192.168.0.51" //(tomcat2
里下
tomcat2
的
ip
啊
)
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.0.51" //(tomcat2
里下
tomcat2
的
ip
啊
)
autoBind="100"
port="5001" //
(
tomcat2
的不一样啊我弄的是
5002
)
selectorTimeout="100"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<!-- <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/> -->
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
Tomcat2
一样的就不说。
Tomcat
的测试(我是先把测试页放了进去的)
②
修改
nginx.conf
user nobody;
worker_processes 1;
#error_liog logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
#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;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_min_length 10;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css application/xml;
#sendfile on;
upstream localhost
{
server 192.168.0.51:8080 weight=1;
server 192.168.0.52:8080 weight=1; //
设置访问的几率数值越大访问的几率就越大
# ip_hash;
}
server {
listen 80; //
监听的端口
server_name localhost; //
服务器的地址可以是
ip
charset utf-8; //
编码格式
# access_log logs/host.access.log main;
location / {
root /usr/local/apache-tomcat- 6.0.29 /webapps/aa; //
要访问页面的路径
index index.jsp index.html index.htm;
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;
proxy_pass http://localhost;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
③
修改
web.xml
在
<web-app> </web-app>
之间加上
<distributable/>
④
在
/usr/local/apache-tomcat- 6.0.29 /webapps
下创建
aa
文件夹并在
aa
里编写一段
index.jsp
的测试页。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
%>
<html>
<head>
</head>
<body>
51
<!--server1
这里为 51 -->
<br />
<%out.print(request.getSession()) ;%>
<!--输出session-->
<br />
<%out.println(request.getHeader("Cookie")); %>
<!--输出Cookie-->
</body>
</html>
测试哈结果
点击刷新数字会在
51
,
52
之间跳动就证明成功了。
参考网址:
http://jlsfwq.blog.51cto.com/818700/195879
http://bbs.linuxtone.org/forum-viewthread-tid-1195-highlight-tomcat%2Bsession%2B%E5%90%8C%E6%AD%A5.html
本人初学
Linux
希望大家多多指教不对之处还请大家指正。在这里还得感谢
summer
,游弋の亡灵。