负载均衡与集群之nginx+tomcat+memcached
来源:互联网 发布:百度软件管家官方下载 编辑:程序博客网 时间:2024/06/08 07:50
近年来nginx越来越流行,很多网站都开始用nginx来做web代理,以及原来的大公司也都在考虑用nginx来代替apache和resign。由 于我在上个公司时,一个电子商务网站开始用的是apache+tomcat,后来也开始考虑用nginx+tomcat+memcache来代替 apache+tomcat。
通用架构如下:
其中用memcache来存储session,tomcat作为servlet容 器,nginx作为tomcat代理,对外接口。这个时候只要保证memcache服务器不停,tomcat之间可以任意热插拔。实现了session的 独立存储,需要的话,还可以存储到数据库或者文件中,也不用考虑是否使用黏性session。
在本例中,使用到了一个memcache服务器,3个tomcat,一个nginx,rhel5.4 64位操作系统,配置如下。
nginx配置:
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;
server_tokens off;
#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;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6].";
proxy_temp_path proxy_temp_dir;
proxy_cache_path proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=3g;
upstream shopcluster{
server 127.0.0.1:8180 weight=1;
server 127.0.0.1:8182 weight=1;
}
upstream hmfkcluster{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=1;
}
server {
listen 80 default;
server_name localhost;
#charset utf-8;
#access_log logs/host.access.log main;
location / {
#root /data/zwlsshop/www;
proxy_pass http://shopcluster;
index index.jsp index.html index.htm;
}
location ~* \.(gif|jpg|png|js|css)$ {
access_log off;
# root /data/zwlsshop/www;
expires 2h;
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid any 1h;
if ( !-f \$request_filename) {
# access_log logs/imgaccess.log;
proxy_pass http://shopcluster;
}
}
# location = /test/cachetest.jsp {
# access_log logs/cachetestall.log;
# root /data/zwlsshop/www;
# expires 2h;
# proxy_cache cache_one;
# proxy_cache_key $host$uri$is_args$args;
# proxy_cache_valid any 1h;
# if ( !-f \$request_filename) {
# proxy_pass http://shopcluster;
# }
# }
# location ~ /purge(/.*) {
# proxy_cache_purge cache_one $host$1$is_args$args;
#
#
# }
location /nginxs {
stub_status on;
access_log 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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
server_name localhost2 <a target="_blank" href="http://www.myroute.cn">www.myroute.cn
</a> #charset utf-8;
#access_log logs/host.access.log main;
location / {
#root /data/zwlsshop/www;
proxy_pass http://hmfkcluster;
index index.jsp index.html index.htm;
}
# location ~* .+\.(gif|jpg|png|js|css)$ {
#
# access_log off;
# root /data/zwlsshop/www;
# expires 2h;
# proxy_cache cache_one;
# proxy_cache_key $host$uri$is_args$args;
# proxy_cache_valid any 1h;
# if ( !-f \$request_filename) {
# access_log logs/imgaccess.log;
# proxy_pass http://hmfkcluster;
# }
# }
#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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
tomcat只需要改动一点,即在conf文件夹下的context.xml添加下面语句
在contex里面添加:
[html] view plaincopyprint?
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="m1:127.0.0.1:11211"
sticky="false"
lockingMode="auto"
sessionBackupAsync="false"
sessionBackupTimeout="1000"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
gt;
其中memcachedNodes指定memcache的ip地址以及端口。sticky指定是否在tomcat中存储session做缓存。可以做两个memcach服务器,以备份session。
- 负载均衡与集群之nginx+tomcat+memcached
- 负载均衡与集群之nginx+tomcat+memcached
- nginx+tomcat+memcached集群、负载均衡
- Nginx+Tomcat负载均衡与集群
- Nginx+Tomcat负载均衡与集群
- Tomcat+Nginx集群与负载均衡
- Tomcat+Nginx集群与负载均衡
- nginx、memcached、tomcat 负载均衡和集群配置,session共享
- tomcat7+nginx+memcached 配置tomcat 集群以及负载均衡
- 基于memcached、nginx的tomcat负载均衡和集群
- tomcat7+nginx+memcached 配置tomcat 集群以及负载均衡
- nginx+tomcat+memcached搭建服务器集群及负载均衡
- nginx+tomcat+memcached负载均衡集群搭建详细笔记(上)
- nginx+tomcat+memcached负载均衡集群搭建详细笔记(下)
- Nginx+Tomcat+Memcached负载均衡集群服务搭建
- nginx+tomcat+memcached负载均衡集群搭建详细笔记(下)
- nginx+tomcat+memcached搭建服务器集群及负载均衡
- Nginx+Tomcat+Memcached负载均衡集群服务搭建
- 同步一篇博客222
- 获取计算后的css样式
- 黑马程序员——iOS 开发学习---循环结构及UI控件
- 总结:Linux下,忘记root密码解决方案
- Android获取手机型号,系统版本,App版本号等信息
- 负载均衡与集群之nginx+tomcat+memcached
- root、oracle用户执行df -h命令卡住
- Qt调用外部exe并同步显示输出
- JavaScript 模拟类(class)的三种方法
- C语言学习之关键字第七讲
- 4395: [Usaco2015 dec]Switching on the Lights|暴力
- p3p协议实现跨域站点登录
- Activity设置全屏显示的两种方式及系统自带theme属性解析
- The specified child already has a parent. You must call removeView() on the child's parent first.