實驗環境:javascript
hostname ipcss
nginx 192.168.1.241html
java1 192.168.1.242java
java2 192.168.1.243nginx
1.nginx+upstream安裝web
[root@nginx ~]# useradd nginx -s /sbin/nologin tomcat
[root@nginx opt]# tar zxf pcre-8.34.tar.gz session
[root@nginx opt]# cd pcre-8.34app
[root@nginx pcre-8.34]# ./configure jsp
[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一直沒變,表示共享成功。