同主机实现
1、安装并启动tomcat
1)OpenJDK的安装
yum install java-1.8.0-openjdk-devel.x86_64
确定JDK是否安装可以使用如下命令
java -version ##可以看到版本,说明安装成功
2)tomcat安装
yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
service tomcat start
ss -ntl
监听的端口有三个:8080为http协议的接口,8009为ajp协议的接口,8005为管理接口
2、安装nginx并配置
vim /etc/nginx/nginx.conf
在server段增加如下内容
root
/usr/local/tomcat/webapps/ROOT; ##表示访问静态文件就访问nginx的这个目录,此目录是tomcat默认根目录。
index index.jsp;
location ~* \.(jsp|do)$ {
##访问的是jsp或者do结尾的动态文件就调度到后端主机tomcat
proxy_pass :8080;
}
nginx -t
service nginx restart
##这里直接访问80端口就可以了看到tomcat默认主页面了
2.实现LAT
同主机(静态网页)
1、安装并启动tomcat
1)OpenJDK的安装
yum install java-1.8.0-openjdk-devel.x86_64
确定JDK是否安装可以使用如下命令
java -version ##可以看到版本,说明安装成功
2)tomcat安装
yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
service tomcat start
ss -ntl
监听的端口有三个:8080为http协议的接口,8009为ajp协议的接口,8005为管理接口
2、安装httpd服务并确保有ajp_module和http_module
httpd -M |grep "proxy"
##查看模块是否已经加载,如果没有加载,要在配置文件中加载这些模块
proxy_module (shared)
proxy_ajp_module (shared) ##表示反向代理时后端服务器是aip协议
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared) ##表示反向代理时后端主机时http协议
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)
3、与后端tomcat使用http协议连接时配置
vim /etc/httpd/conf.d/tomcat.conf
<virtualhost *:80>
documentroot "/usr/share/tomcat/webapps/ROOT"
<directory "usr/local/tomcat/webapps/ROOT">
require all granted
</directory>
proxyrequests off
##关闭正向代理,不加可以
proxyvia on
proxypreservehost on
proxypass "http://www.likecs.com/" "http://127.0.0.1:8080/"
proxypassreverse "http://www.likecs.com/" "http://127.0.0.1:8080/"
</virtualhost>
httpd -t
systemctl start httpd
测试:
4、与后tomcat连接时使用的ajp协议时的配置
vim /etc/httpd/conf.d/tomcat.conf
<virtualhost *:80>
documentroot "/usr/local/tomcat/webapps/ROOT"
<directory "usr/local/tomcat/webapps/ROOT">
require all granted
</directory>
proxyrequests off
proxyvia on
proxypreservehost on
proxypass "http://www.likecs.com/" "ajp://127.0.0.1:8009/"
proxypassreverse "http://www.likecs.com/" "ajp://127.0.0.1:8009/"
</virtualhost>
httpd -t
systemctl reload httpd
5、测试
3.实现lnmt的zrlog博客搭建
环境:
A主机:tomcat,nginx,ip地址为192.168.213.251
B主机:数据库,ip地址为192.168.213.253
1)在A上操作
1、安装openjdk
yum install java-1.8.0-openjdk-devel.x86_64
java -version ---确认是否安装成功
2、安装tomcat
yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
systemctl start tomcat
:8080 ##可以访问tomcat的默认网站
3、将zrlog放到tomcat的根目录下
mv app/zrlog-1.7.1-release.war /usr/share/tomcat/webapps
systemctl restart tomcat
ls ##发现文件被自动解开
docs
host-manager manager zrlog-1.7.1-release
examples logs
ROOT
zrlog-1.7.1-release.war
[root@centos7 webapps]#ln -s zrlog-1.7.1-release zrlog
4、安装nginx并修改配置
vim /etc/nginx/nginx.conf
在server段增加如下内容
root
/usr/local/tomcat/webapps; ##表示访问静态文件就访问nginx的这个目录,此目录是tomcat默认根目录。
index index.jsp;
location / {
##访问的是jsp或者do结尾的动态文件就调度到后端主机tomcat,此处为本地
proxy_pass :8080;
}
nginx -t
service nginx restart
##这里直接访问80端口就可以了看到tomcat默认主页面了
2)在B上的设置
安装数据库
yum install mariadb-server
systemctl start mariadb
mysql
create database zrlog;
grant all on zrlog.* to zrloguser@\'%\' identified by "centos";
3)测试:
登录网站
4.实现nginx和httpd反代tomcat集群
环境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254
1》nginx反代
1、在B和C上的设置
mkdir -pv /usr/share/tomcat/webapps/myapp/WEB-INF
vim /usr/share/tomcat/webapps/myapp/index.jsp
<%@ page language="java" %>
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.com</font></h1> ##在C上将颜色改为green,Tomcat改为B
<table>
<tr>
<td>Session ID</td>
<% session.setAttribute("shen.com","shen.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
:8080/myapp/
:8080/myapp/
2、A上设置
vim /etc/nginx/nginx.conf
在http中配置
upstream tomcatsrv {
server 192.168.213.251:8080 ;
server 192.168.213.254:8080 ;
}
server {
listen 80 default_server;
listen
[::]:80 default_server;
index index.jsp ;
root /usr/share/tomcat/webapps/app;
location / {
proxy_pass ;
}
service nginx restart
2》httpd反代
1、在B和C上的设置同上
2、在调度器上的设置
vim /etc/httpd/conf.d/tomcat.conf
<proxy balancer://tomcatsrvs>
##定义一个后端服务器组
BalancerMember :8080 ##如果和后端服务器连接的协议为ajp协议,把http改为ajp并且把端口改为8009即可
BalancerMember :8080
ProxySet lbmethod=byrequests
</proxy>
namevirtualhost *:80
<VirtualHost *:80>
documentroot /app
<Directory /app>
Require all granted
</Directory>
ProxyVia On
ProxyRequests Off
ProxyPass / balancer://tomcatsrvs/
ProxyPassReverse / balancer://tomcatsrvs/
</VirtualHost>
httpd -t
service httpd start
httpd的负载集群功能具有健康状态检查功能
可以把后端的一个tomcat停掉
然后访问
会发现不往关闭的tomcat主机调度了
5.实现httpd和nginx的会话粘性绑定