1 安装nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.6.0.tar.gz
[root@localhost ~]# yum -y install pcre-devel (nginx支持正则表达式)
[root@localhost ~]# yum -y install gcc-c++ zlib-devel
[root@localhost ~]# tar xvf nginx-1.6.0.tar.gz
[root@localhost ~]# cd nginx-1.6.0
[root@localhost nginx-1.6.0]# ./configure --with-file-aio --with-http_stub_status_module --prefix=/usr/local/nginx
[root@localhost nginx-1.6.0]# make && make install
配置:
[root@localhost conf]# vi nginx.conf
worker_processes 4;
events {
use epoll;
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/minxin_nginx_access.log main;
sendfile on;
tcp_nopush on;
aio on;
directio 512;
output_buffers 1 128k;
keepalive_timeout 65;
proxy_cache_path ./cache levels=1:2 keys_zone=cache1:100m inactive=1d max_size=800m;
proxy_temp_path ./proxy;
gzip on;
upstream tomcat_cluster {
server 10.255.33.53:8080 max_fails=3 fail_timeout=30s;
server 10.255.33.54:8080 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name 10.255.33.51;
access_log logs/nginx_ha_host.access.log main;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_cluster;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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;
}
location /status {
stub_status on;
access_log off;
allow 192.168.7.42;
deny all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache1;
proxy_cache_valid 200 304 1h;
proxy_cache_valid 301 302 5m;
proxy_cache_valid any 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Accept-Encoding "";
proxy_ignore_headers "Cache-Control" "Expires";
proxy_pass http://tomcat_cluster;
expires 15m;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
说明:
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
2 安装memcache
[root@localhost ~]# wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
[root@localhost ~]#wget http://memcached.org/files/memcached-1.4.20.tar.gz
(1)memcache依赖libevent
tar xvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr
make
make install
(2)安装memcache
tar xvf memcached-1.4.20.tar.gz
cd memcached-1.4.20
./configure --with-libevent=/usr
make
make install
启动
[root@localhost memcached-1.4.20]# /usr/local/bin/memcached -d -m 256 -u root -p 11211 -c 1024 -P /tmp/memcached.pid
telnet 127.0.0.1 11211stats
3 tomcat 配置(安装略)
(1)放入tomcat安装目录下的lib/目录下
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/1.8.1/memcached-session-manager-tc7-1.8.1.jar
wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.10.2/spymemcached-2.10.2.jar
wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/1.8.1/memcached-session-manager-1.8.1.jar
wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/1.8.1/msm-kryo-serializer-1.8.1.jar
wget http://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.11/kryo-serializers-0.11.jar
wget http://repo1.maven.org/maven2/com/googlecode/kryo/1.04/kryo-1.04.jar
wget http://repo1.maven.org/maven2/com/googlecode/minlog/1.2/minlog-1.2.jar
wget http://repo1.maven.org/maven2/com/googlecode/reflectasm/1.01/reflectasm-1.01.jar
wget http://repo1.maven.org/maven2/asm/asm/3.2/asm-3.2.jar
(2)context.xml 加入
vi /usr/local/tomcat/conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:10.255.33.51:11211"
sticky="false"
sessionBackupAsync="false"
lockingMode="all"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
vi /usr/local/tomcat/conf/logging.properties 加入tomcat有带有sessionID的日志
net.spy.memcached.level = WARNING
4 Keepalived 安装及配置
yum install openssl*
[root@localhost ~]# wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
55 wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
60 yum -y install openssl*
56 tar xvf keepalived-1.2.13.tar.gz
58 cd keepalived-1.2.13
59 ./configure --prefix=/usr/local/keepalived
62 make && make install
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cd /etc/keepalived
vi keepalived.conf
global_defs {
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER ####主用MASTER,从用BACKUP
interface eth0 ####监测的网络接口
virtual_router_id 51 ####主从上要设置一样的值
#backup ipaddress
mcast_src_ip 10.1.1.191 ####主机的IP,备机更换备机用的IP
priority 100 ####权重,备机比主机稍微调小一点就行
advert_int 1 ####VRRP Multicast广播周期秒数
authentication {
auth_type PASS ####VRRP认证方式
auth_pass qwerty ####认证密码
}
virtual_ipaddress {
10.1.1.190 ####VRRP
}
}
global_defs {
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
#backup ipaddress
mcast_src_ip 10.1.1.191
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass qwerty
}
virtual_ipaddress {
10.1.1.190
}
}