nginx+tomcat实现集群负载均衡(实现session复制)
架构描述
前端一台nginx服务器做负载均衡器,后端放N台tomcat组成集群处理服务,通过nginx转发到后面(注:没做动静分离,静态动态全部都转给tomcat)
优点:实现了可弹性化的架构,在压力增大的时候可以临时添加tomcat服务器添加到这个架构里面去
一,配置nginx
1, 下载包
Wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
2, 安装nginx包
a.安装pcre
tar zxvf pcre-7.2.tar.gzcd pcre./configure –prefix = /pcreMake;make install
tar zxvf nginx-0.6.32.tar.gzcd nginx-0.6.32./configure –prefix=/nginx –with-pcre=/pcre –with-http_rewrite_moduleMake;make install
Vi /nginx/conf/nginx.conf#用户组user nobody nobody;#cpu个数,可以按照实际服务器来计算worker_processes 8;worker_rlimit_nofile 51200;events {use epoll;#连接数worker_connections 8192 ;}http {include mime.types;default_type application/octet-stream;server_names_hash_bucket_size 128;# access_log off;# access_log logs/access.log;#缓存的时间,(可以根据不同文件设置不同时间)# expires 2h;tcp_nodelay on;keepalive_timeout 30;gzip on;gzip_min_length 10;gzip_buffers 4 8k;gzip_http_version 1.1;gzip_types text/plain application/x-javascript text/css text/html application/xml;sendfile on;tcp_nopush on;reset_timedout_connection on;client_max_body_size 30m;#设定负载均衡列表upstream backend{server 172.23.254.2:8080;server 172.23.254.3:8080;}#设定虚拟主机server {listen 80;server_name www.abc.com;#对 / 所有做负载均衡(本机nginx采用完全转发,所有请求都转发到后端的tomcat集群)location / {root /web/www ;index index.jsp index.htm index.html;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://backend;}}}/nginx/sbin/nginx
Vi nginx.sh#!/bin/shCWD=`pwd`case $1 instart)/nginx/sbin/nginx;;;stop)kill -2 `ps -ef|grep “/nginx/sbin/nginx”|grep -v “grep”|awk ‘{print $2}’ `;;restart)cd “$CMD”$0 stop$0 start;;*)echo $”Usage: $0 {start|stop|restart}”exit 1esacexit 0maxThreads="2048" minSpareThreads="100" maxSpareThreads="200"enableLookups="false" redirectPort="8443" acceptCount="500"connectionTimeout="20000" disableUploadTimeout="true" />
User user = (User)request.getSession().getAttribute(“user”);User.setName(“my name”);