nginx+tomcat集群及使用memcache支持session共享 配置说明

Nginx+tomcat集群配置

1 环境条件

需要的环境条件有:jdk,tomcat,nginx等已安装好。本文是tomcat6版本

2 配置nginx

# cd /usr/local/nginx
# vim conf/nginx.conf               (编辑nginx的配置文件如下)
#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 www.client.com {
        server localhost:8080;
        server localhost:8081;
    }
 
   server {
       listen       80;
     
      server_name  www.client.com;
 
       #charset koi8-r;
 
       #access_log logs/host.access.log  main;
 
       location / {
           root   html;
           index  index.html index.htm;
           proxy_passhttp://www.client.com;
           proxy_set_header  X-Real-IP $remote_addr;
        }
 
       #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;
   #    }
    #}
 
}


配置项说明:

upstream 指定绑定的tomcat集群名字和对应的tomcat的地址及端口号,以上配置的内容是在本机8080端口和8081端口的两个tomcat的集群。

proxy_pass 指定代理的集群名字,http://后面填写的内容需要和upstream指定的名字一样。

server_name 指定绑定的域名,listen指定绑定的端口号。

3        tomcat配置

需要配置tomcat目录下conf/server.xml

1)修改

节点,每个tomcat的port端口号不能一样。


2)修改

节点,每个tomcat的port和redirectPort不能一样。


3) 修改

节点,每个tomcat的port和redirectPort不能一样。


4)  如果以下https协议的配置被放开的话,则修改上面2,3步的端口和此处一样。且每个tomcat的端口不能一样。



4        Session共享,Memcache安装

4.1       Memcached安装

需有libevent支持,提前安装好。

# tar zxvf memcached-1.4.5.tar.gz

# cd memcached-1.4.5/

# ./configure  

# make      

# make install

# ./memcached -d -m 2048 -l localhost -p 11211      (启动)

这会以守护程序的形式启动 memcached(-d),为其分配 2GB 内存(-m 2048),并指定监听(-l localhost),即端口 11211。您可以根据需要修改这些值。

启动错误解决:

1启动报错 error while loading sharedlibraries: libevent-2.0.so.5: cannot open shared object file: No such file ordirectory

# whereis libevent-2.0.so.5    (找到此文件位置)

# ldd /usr/local/bin/memcached  (ldd命令查找相关依赖文件信息)

libevent-2.0.so.5 => not found   (没有找到该文件)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b83fce0e000)
libc.so.6 => /lib64/libc.so.6 (0x00002b83fd029000)
librt.so.1 => /lib64/librt.so.1 (0x00002b83fd381000)
/lib64/ld-linux-x86-64.so.2 (0x00002b83fc9b0000)

# LD_DEBUG=libs ./memcached -v

     

 9020:    find library=libevent-2.0.so.5 [0]; searching
      9020:      search cache=/etc/ld.so.cache
      9020:      searchpath=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib            (system search path)
      9020:       tryingfile=/lib/tls/i686/sse2/libevent-2.0.so.5
      9020:       tryingfile=/lib/tls/i686/libevent-2.0.so.5
      9020:       tryingfile=/lib/tls/sse2/libevent-2.0.so.5
      9020:       trying file=/lib/tls/libevent-2.0.so.5
      9020:       tryingfile=/lib/i686/sse2/libevent-2.0.so.5
      9020:       trying file=/lib/i686/libevent-2.0.so.5
      9020:       trying file=/lib/sse2/libevent-2.0.so.5
      9020:       trying file=/lib/libevent-2.0.so.5
      9020:      trying file=/usr/lib/tls/i686/sse2/libevent-2.0.so.5
      9020:       tryingfile=/usr/lib/tls/i686/libevent-2.0.so.5
      9020:       tryingfile=/usr/lib/tls/sse2/libevent-2.0.so.5
      9020:       trying file=/usr/lib/tls/libevent-2.0.so.5
      9020:       tryingfile=/usr/lib/i686/sse2/libevent-2.0.so.5
      9020:       tryingfile=/usr/lib/i686/libevent-2.0.so.5
      9020:       tryingfile=/usr/lib/sse2/libevent-2.0.so.5
      9020:       trying file=/usr/lib/libevent-2.0.so.5
      9020:   ./memcached:error while loading shared libraries: libevent-2.0.so.5: cannot open sharedobject file: No such file or directory

从最后一行可以看出,此命令执行后,所查找的路径为/usr/lib/libevent-2.0.so.5

而该文件不存在此目录下,因此我们只需要做个软链接到该路径即可。

# ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5

# ldd /usr/local/bin/memcached  (再执行此命令看看,发现好了,接着再执行启动命令即可。)


2.启动报错can't run as root without the-u switch

如果没有带 -u root 的话就会报:

can't run as root without the -u switch

解决:带-u root就行!记着,-u root 加在最后

# ./memcached -d -m 2048 -l localhost -p 11211 -u root

4.2       Tomcatsession共享配置

1.     将这5个包放到$TOMCAT_HOME/lib目录下

javolution-5.4.3.1.jar

memcached-session-manager-1.6.3.jar

memcached-session-manager-tc6-1.6.3.jar

msm-javolution-serializer-1.6.3.jar

spymemcached-2.7.3.jar

       记住,包的版本必须一样,这里使用的是1.6.3的版本,此几个包支持的tomcat6的版本,如果需要支持tomcat7,可以在官方下载memcached-session-manager-tc7-1.6.3jar替换掉memcached-session-manager-tc6-1.6.3jar包,另外还有更多细节可以修改,这里不做过多介绍,更多的说明可详细查看官网的说明。

打包下载:http://download.csdn.net/detail/fengge374043291/8128493

单个下载地址:http://repo1.maven.org/maven2/de/javakaffee/msm/

官网msm介绍:https://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration

2.     修改$TOMCAT_HOME/conf/server.xml

 
           


这里的memcachedNodes是填写memcached节点,多个节点时可以以空隔分开,如:

 n1:localhost:11211 n2:localhost:11212

 sessionBackupTimeout的单位为分钟

 /www/tomcat-6.0.18/webapps/WebContent替换成你的WEB目录

 修改后重启两个TOMCAT即可,这个时候已经解决SESSION的共享问题.

最后使用tomcat集群测试即可。

 

提示:网上有好多相关的配置等文章,但是并不一定能成功,可能各个环境不一定一样,所以很有可能最后未能有效,本人也是搜集了好多文章慢慢总结出来的,并且在我本地测试成功了,建议还是多参考官网的文章说明,比较权威,个人博客等仅供参考比较好。

你可能感兴趣的:(集群,memcache)