最近在研究tomcat 集群相关知识,网上百度也一大堆,结合百度和自己的实践,总结一下:
1.nginx 配置,配置nginx 多tomcat
(1)主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 {
#gzip默认最低支持1.1现在改成最低支持1.0 近代浏览器基本不设置
gzip_http_version 1.0;
#gzip最小长度 一般设置成1K就行,小于1K的就不压缩了 不然会越压越大
gzip_min_length 1024;
#默认值: gzip_buffers 4 4k/8k
#作用域: http, server, location
#设置系统获取几个单位的缓存用于存储gzip的压缩结 果数据流。 例如 4 4k 代表以4k为单位,按照原始数据大小以4k为单位的4倍申请内存。 4 8k 代表以8k为单位,按照原始数据大小以8k为单位的4倍申请内存。
#如果没有设置,默认值是申请跟原始数据相同大小的内存空间去存储gzip压缩结果。
gzip_buffers 4 16k;
#gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢(传输快但比较消耗cpu)。
gzip_comp_level 6;
#匹配MIME类型进行压缩
gzip_types text/plain application/json application/javascript;
gzip_vary on;
#语法: gzip_proxied [off|expired|no-cache|no-store|private|no_last_modified|no_etag|auth|any] ...
#默认值: gzip_proxied off
#作用域: http, server, location
#Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。
#off - 关闭所有的代理结果数据的压缩
#expired - 启用压缩,如果header头中包含 "Expires" 头信息
#no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息
#no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息
#private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息
#no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
#no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
#auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息
#any - 无条件启用压缩
gzip_proxied expired no-cache no-store private auth;
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;
upstream sso{
server localhost:8089;
server localhost:8092;
}
#upstream lgww{
# server localhost:8080;
#}
include vhosts/*.conf;
log_format main '$remote_addr $upstream_addr - $remote_user [$time_local] "$request" $status $bytes_sent $request_time "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
server {
listen 90;
server_name localhost;
#charset koi8-r;
access_log logs/access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location /sso {
proxy_pass http://sso/sso;
}
#location / {
# proxy_redirect off;
# # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
# proxy_set_header Host $host:$server_port;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
# proxy_pass http://127.0.0.1:8080/;
#}
#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;
# }
#}
}
(2)包含的lgy.conf 配置如下
upstream lgww{
server localhost:8080 weight=1;
server 192.168.1.192:8083 weight=1;
}
log_format lgwwlog '$remote_addr $upstream_addr - $remote_user [$time_local] "$request" $status $bytes_sent $request_time "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
server {
listen 83;
server_name localhost;
#charset koi8-r;
access_log logs/lgww-access.log lgwwlog;
error_log logs/lgww-error.log;
location /lgww {
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_pass http://lgww/lgww;
}
location /lgnw {
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_pass http://lgww/lgnw;
}
location /qxgl {
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_pass http://lgww/qxgl;
}
}
3.tomcat 采用tomcat8,集群配置需要修改server.xml配置文件,如下:
在
下面加如下配置:
4.需要在项目的web.xml中配置
注意该配置需要session里面的数据实现序列化才行
例如:
public class BaseUser implements java.io.Serializable
好了到此已经配置完成了,启动nginx,tomcat 即可,测试session共享说明配置成功