環境介紹:兩臺win2008_x64位主機+jdk7_x64位 分別爲A主機IP:192.168.7.100、B主機IP:192.168.7.101php
1、首先安裝memcached(A主機、B主機都須要安裝)css
下載地址:http://download.csdn.net/detail/ttyyadd/9414586html
下載完成後解壓到本地磁盤。java
打開cmd命令窗口進行到memcached.exe所在目錄,而後執行下面命令進行安裝nginx
memcached.exe -d install
安裝後並無啓動須要到服務管理裏面找到memached服務將它啓動。web
2、下面開始配置tomcat6 緩存
一、首先下載所須要jartomcat
將下載的jar放到tomcat目錄下的lib文件夾下面session
http://download.csdn.net/detail/ttyyadd/9414529app
二、開始配置(A主機:192.168.7.100)context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.7.100:11211,n2:192.168.7.101:11211" failoverNodes="n2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
開始配置(B主機:192.168.7.101)context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.7.100:11211,n2:192.168.7.101:11211" failoverNodes="n1" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
這裏要注意的是failoverNodes屬性,該屬性值要指向(非本機的)另一臺主機。
三、新建一個test.jsp頁面,分別放到A主機和B主機的Tomcat 下的ROOT目錄
<%@ page language="java" %> <%@ page import="java.util.*" %> <html><head><title></title></head> <body> <% System.out.println(session.getId()); out.println("<br> HostA HostB, SESSION ID:" + session.getId()+"<br>"); %> </body> </html>
3、配置nginx
一、首先下載 nginx安裝包
http://nginx.org/
二、開始配置nginx
打開安裝目錄的nginx.conf。個人安裝目錄爲(D:\nginx-1.9.9\conf\nginx.conf)
#user nobody; worker_processes 1; #error_log 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; #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; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream mysvr { #weigth參數表示權值,權值越高被分配到的概率越大 #1.down 表示單前的server暫時不參與負載 #2.weight 默認爲1.weight越大,負載的權重就越大。 #3.backup: 其它全部的非backup機器down或者忙的時候,請求backup機器。因此這臺機器壓力會最輕。 #server 192.168.1.116 down; #server 192.168.1.116 backup; server 192.168.7.100:8080 weight=1; server 192.168.7.101:8080 weight=1; } server { listen 80; server_name 192.168.7.100; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://mysvr; proxy_connect_timeout 5s; 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_buffering off; } #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 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
保存並關閉。nginx.conf 文件內容比較多,其實只須要修改紅色字體部分
而後在cmd窗口進入安裝目錄啓動nginx命令是:
start nginx
nginx其它相關命令:
1) 啓動Nginx:start nginx
2) 中止Nginx:nginx -s stop
3) 修改配置後重啓:nginx -s reload
到如今已經配置完成
開始訪問:http://192.168.7.100/test.jsp
能夠看到session id不會改變。