测试环境 nginx1.14, tomcat8, jdk1.8
nginx1.14可以去官网下载,官网地址:http://nginx.org/en/download.html
或者可以去我的资源库下载:https://download.csdn.net/download/heqinghua217/10955843
集群配置参考文章:
参考1、
https://blog.csdn.net/yulei_qq/article/details/52759323
参考2、
https://blog.csdn.net/donggang1992/article/details/51095551
nginx的集群比apache简单一点,不过目前有一个问题,就是session共享有点问题,说是要引入第三方的jvmroute,但是这个安装包只支持linux,所以没有测试,目前只能做到ip_hash的方式分发,这种方式可以实现一台服务器挂了,会自动转发到第二台,但是我测试了很多遍,发现了一个bug,两个tomcat,4台不同ip的电脑,访问,无数次刷新,永远都是访问到tomcat2,而不是tomcat1,权重是一样的配置。
第二种就去掉ip_hash, 采用默认的分流方式,如果两个tomcat的权重一样,那么你访问nginx的时候,刷新一次就访问到不同的tomcat上,那么问题来了,要登录怎么办你再这边登录了,点击查询按钮,访问后台,就到了另一个tomcat,那么又得登录。所以目前windows上没有找到好的解决办法,只能是在linux上引入第三方的jvmroute来解决。
tomcat的配置我就不写了,可以看我那边关于apache集群-自己整理的那份博客。这里只附上 nginx的配置文件说明:
#使用的用户和组
#user nobody;
#指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍,例如两个四核CPU,则综合数为8.通过命令ps -ef|grep nginx可以看出来设置的是几个)
worker_processes 2;
#指定错误日志存放的路径,错误日志记录级别可选项为:[debug|info|notice|warn|error|crit],默认是crit,记录的日志数量从crit到debug,由少到多。
#
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定pid存放的路径
#pid logs/nginx.pid;
#使用的网络I/O模型,Linux系统推荐采用epoll模型,FreeeBSD系统推荐采用kqueue模型
events {
#允许的连接数
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/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#长连接超时时间,单位是秒
keepalive_timeout 65;
#gzip on;
#集群设置 --此段手动添加进来 clustServerTomcat 在后面代理中用到, 默认是轮训,
#如果是ip_hash需要注意ip的变动问题,以及转发问题,局域网还好,如果局域网映射到其他网段,出去的是同一个ip,那就没有意义了
upstream clustServerTomcat{
ip_hash;
server 10.250.196.100:8888 weight=1;
server 10.250.196.68:8888 weight=1;
}
server {
listen 8080;
server_name localhost;
#设置使用的字符集,如果一个网站有多种字符集,请不要随便设置,应让程序员在HTML代码中通过Meta标签设置
#charset koi8-r;
#access_log logs/host.access.log main;
#注意修改后缀,修改成你支持的后缀,如jsp, html, shtml之类的
location / {
root html;
index index.html index.jsp;
#启动代理 (clustServerTomcat 这个名称是上面upstream设置的名称,两个要一致 )
proxy_pass http://clustServerTomcat;
#添加如下3个配置后,当一台server宕机,切换速度会很快,此时配置是1秒
proxy_connect_timeout 1;
proxy_send_timeout 1;
proxy_read_timeout 1;
}
#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
#
#启动代理 (clustServerTomcat 这个名称是上面upstream设置的名称,两个要一致 )
#location ~ \.php$ {
# proxy_pass http://clustServerTomcat;
#}
# 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;
# }
#}
}