一:tomcat安装(在192.168.10.102和192.168.10.103)
1:关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
2:安装java环境
[root@localhost ~]# yum -y install java
[root@localhost ~]# java -version
3:安装tomcat
[root@localhost ~]# tar zxvf apache-tomcat-9.0.8.tar.gz
[root@localhost ~]# mv apache-tomcat-9.0.8 /usr/local/tomcat8
4:启动tomcat
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
[root@localhost ~]# netstat -anpt | grep java
5:创建测试网页
[root@localhost ~]# mkdir -p /web/webapp1
[root@localhost ~]# vim /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
</body>
<body>
<div>动态页面的图片 1</div><br><img src="logo.jpg">
</body>
</html>
6:修改tomcat主配置文件
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp1" path="" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
7:重启tomcat
[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
8:客户端访问测试
http://192.168.10.102:8080 ##注意:此时看不到图片,图片要放到nginx上,由nginx解析
备注:
另外一台tomcat用同样的方法部署
二:nginx配置(在192.168.10.101)
1:安装nginx
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc*
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar zxvf nginx-1.12.0.tar.gz
[root@localhost ~]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
2:编辑测试网页文件
[root@localhost ~]# vim /usr/local/nginx/html/index.html
<body>
<h1>静态页面</h1>
<p>这是个静态页面</p>
</body>
3:添加图片
[root@nginx ~]# mkdir /usr/local/nginx/html/img //创建静态文件目录
[root@nginx ~]# cp /root/logo.jpg /usr/local/nginx/html/img
4:修改主配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
(1)在http配置项中添加
upstream tomcat_server {
server 192.168.10.102:8080 weight=1;
server 192.168.10.103:8080 weight=1;
}
(2)在server配置项中添加
charset utf-8;
location / {
root html;
index index.html index.htm;
}
location ~ .*\.jsp$ {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat_server;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
root /usr/local/nginx/html/img;
expires 30d;
}
备注:
proxy_set_header HOST $host;
把原http请求中的Header中的Host字段放到转发的请求报文里,后端的web服务器利用这个host判断请求的是哪个虚拟主机
proxy_set_header X-Real-IP $remote_addr;
使用$remote_addr变量获得用户的真实ip,$remote_addr一般都是设置第一个代理上面。当一个请求通过多个代理服务器时,用户的IP将会被代理服务器IP覆盖
proxy_set_header Client-IP $remote_addr;
获取客户端ip地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
从客户的真实ip为起点,穿过多层级的proxy ,最终到达web 服务器,都会记录下来。所以在获取用户真实ip的时候,最后一个nginx配置此变量,用来获取客户端的ip地址
5:启动nginx
[root@localhost ~]# nginx
6:修改tomcat的配置文件,并重启tomcat
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%a %r %t %{X-Real-IP}i" resolveHosts="false" />
注意:
在nginx配置文件中用X-Real-IP保存了客户端的真是IP地址,在tomcat中就需要将X-Real-IP解析出来,显示到tomcat的日志中。
6:测试
浏览器打开网站
(1)静态网站测试
(2)动态网站测试
http://192.168.10.101/index.jsp
(3)查看tomcat的日志文件
标签:tomcat,Tomcat,nginx,群集,local,Nginx,usr,root,localhost From: https://blog.csdn.net/weixin_71499831/article/details/140292271cat /usr/local/tomcat9/logs/localhost_access_log-07-09.txt