nginx tomcat group
===================================================
准备:
windows版本的nginx
两个tomcat
1.tomcat配置:
修改tomcat的配置文件server.xml,修改端口
1.<Server port="8005" shutdown="SHUTDOWN">
<Server port="8105" shutdown="SHUTDOWN">
2.<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8180" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
3.<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />
建一个web项目:nginx-tomcat,加入一个test.jsp页面,将项目分别放到两个tomcat webapps下
tomcat1中页面内容为:tomcat1--test.jsp
tomcat2中页面内容为:tomcat2--test.jsp
2.nginx配置:
解压nginx,
在conf下nginx.conf配置文件
文件index.html,内容:nginx--index.html
文件test.html,内容:nginx--test.html
启动两个tomcat(bin/startup.bat),
启动nginx(双击nginx.exe)
浏览器访问:
1.localhost:80 期望访问到index.html:nginx--index.html
2.localhost:80/test.html 期望访问到test.html:nginx--test.html
3.localhost:80/nginx-tomcat/test.jsp 期望访问到tomcat1/tomcat2:tomcat1--test.jsp/tomcat2--test.jsp
负载均衡:多次访问localhost:80/nginx-tomcat/test.jsp,访问tomcat1或tomcat2的概率符合nginx.conf配置文件中: #weigth配置权重
===================================================
准备:
windows版本的nginx
两个tomcat
1.tomcat配置:
修改tomcat的配置文件server.xml,修改端口
1.<Server port="8005" shutdown="SHUTDOWN">
<Server port="8105" shutdown="SHUTDOWN">
2.<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8180" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
3.<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />
建一个web项目:nginx-tomcat,加入一个test.jsp页面,将项目分别放到两个tomcat webapps下
tomcat1中页面内容为:tomcat1--test.jsp
tomcat2中页面内容为:tomcat2--test.jsp
2.nginx配置:
解压nginx,
在conf下nginx.conf配置文件
#Nginx所用用户和组,window下不指定
#user niumd niumd;
#工作的子进程数量(通常等于CPU数量或者2倍于CPU)
worker_processes 2;
#错误日志存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#指定pid存放文件
pid logs/nginx.pid;
events {
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
#use epoll;
#允许最大连接数
worker_connections 2048;
}
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 off;
access_log logs/access.log;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 75 20;
include gzip.conf;
include proxy.conf;
upstream localhost {
#根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
#同一机器在多网情况下,路由切换,ip可能不同
#ip_hash;
#weigth参数表示权值,权值越高被分配到的几率越大
server localhost:8080 weight=1;
server localhost:8180 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
root E:/group/nginx_tomcat_Group/NginxGroup/static;
index index.html index.htm;
}
location ~ \.(html|js|css|png|gif)$ {
root E:/group/nginx_tomcat_Group/NginxGroup/static;
}
location ~ \.(jsp|action)$ {
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://localhost;
}
}
}
建目录E:/group/nginx_tomcat_Group/NginxGroup/static,(用来放静态文件)
文件index.html,内容:nginx--index.html
文件test.html,内容:nginx--test.html
启动两个tomcat(bin/startup.bat),
启动nginx(双击nginx.exe)
浏览器访问:
1.localhost:80 期望访问到index.html:nginx--index.html
2.localhost:80/test.html 期望访问到test.html:nginx--test.html
3.localhost:80/nginx-tomcat/test.jsp 期望访问到tomcat1/tomcat2:tomcat1--test.jsp/tomcat2--test.jsp
负载均衡:多次访问localhost:80/nginx-tomcat/test.jsp,访问tomcat1或tomcat2的概率符合nginx.conf配置文件中: #weigth配置权重
3.tomcat宕机问题
proxy_connect_timeout
配置nginx连接tomcat超时时间,超时后,nginx将请求转发给其它节点;
tomcat修复重启后,nginx可以继续将请求转发给该tomcat;
可以拿上面的例子做测试。
4.反向代理
代理就代理,还不明白为什么叫反向代理。
5.session共享
可以选择memcached、redis等缓存实现session共享;
注意:当tomcat中有定时任务跑时,看具体业务是不是定时任务只能跑一次同一时刻。