tomcat接入负载均衡
负载均衡器上的nginx配置文件如下:
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy_zrlog.wenjie.com.conf
upstream zrlog {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 80;
server_name zrlog.wenjie.com;
#tomcat启动的java程序,可以直接通过proxy_pass进行负载均衡
location / {
proxy_pass http://zrlog;
include proxy_params;
}
}
单节点tomcat配置https
配置文件内容如下
<Connector port="443" #port属性根据实际情况修改(https默认端口为443)。如果使用其他端口号,则您需要使用https://yourdomain:port的方式来访问您的网站。
protocol="HTTP/1.1"
SSLEnabled="true"
scheme="https"
secure="true"
keystoreFile="Tomcat安装目录/cert/domain name.pfx" #证书名称前需加上证书的绝对路径,请使用您证书的文件名替换domain name。
keystoreType="PKCS12"
keystorePass="证书密码" #请替换为密码文件pfx-password.txt中的内容。
clientAuth="false"
SSLProtocol="TLSv1+TLSv1.1+TLSv1.2"
ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256"/>
集群tomcat配置https
模拟生成证书:
mkdir /etc/nginx/ssl_key
cd /etc/nginx/ssl_key/
openssl genrsa -idea -out server.key 2048
openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
修改负载均衡服务器上的nginx配置文件为如下:
upstream zrlog {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 443 ssl;
server_name zrlog.wenjie.com;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass http://zrlog;
include proxy_params;
}
}
server {
listen 80;
server_name zrlog.wenjie.com;
}
配置tomcat 的session会话保持
--------------------------------------------------------准备web01 和 web02 支持redis session共享
1.拷贝所有的jar包
[root@web01 ~]# cp tomcat-cluster-redis-session-manager/lib/* /soft/tomcat/lib/
2.拷贝redis配置文件,并且更新redis配置文件
[root@web01 ~]# cp tomcat-cluster-redis-session-manager/conf/redis-data-cache.properties /soft/tomcat/conf/
3.Add the below two lines in your tomcat/conf/context.xml file.
--------------------------------------------------------准备web01 和web02 一个session测试页面
<Host name="session.oldxu.com" appBase="/session"
unpackWARs="true" autoDeploy="true">
</Host>
[root@web02 ~]# mkdir -p /session/ROOT
[root@web02 ~]# vi /session/ROOT/index.jsp
web02 SESSION ID:" + session.getId() + "
"); out.println("Session created time is :" + session.getCreationTime() + "
"); %>
--------------------------------------------------------接入负载均衡
[root@lb01 conf.d]# cat /etc/nginx/conf.d/proxy_session.oldxu.com.conf
upstream session {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 80;
server_name session.oldxu.com;
location / {
proxy_pass http://session;
include proxy_params;
}
}
建议:
1.先配置session的网站
2.搭建nginx+tomcat集群
3.测试访问,演示是否web01和web02的session不一致
4.接入redis,实现tomcat共享会话信息
5.再次测试,请求web01和web02 看session是否是一致的。
6.登陆redis查看是否存在对应的session的key