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://bbs.linuxtone.org/forum-viewthread-tid-1195-highlight-tomcat%2Bsession%2B%E5%90%8C%E6%AD%A5.html
本人初學
Linux
但願你們多多指教不對之處還請你們指正。在這裏還得感謝
summer
,遊弋の亡靈。