系统相关
首页 > 系统相关> > Nginx+Tomcat虚拟主机、负载均衡、动静分离

Nginx+Tomcat虚拟主机、负载均衡、动静分离

作者:互联网

1. 服务规划

Nginx 服务器:192.168.80.20:80
Tomcat服务器1:192.168.80.25:8080 192.168.80.30:8081
Tomcat服务器2:192.168.80.30:8080

1.1安装nginx服务

1、安装依赖包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make

2、创建运行用户
useradd -M -s /sbin/nologin nginx

3、编译安装
cd /opt
tar zxvf nginx-1.12.2.tar.gz -C /opt/

cd nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

make -j4&& make install

4、优化路径
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

5、添加 Nginx 系统服务
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service

1.2部署2台Tomcat 应用服务器

1.2.1 .25主机配置

1.配置java环境
systemctl stop firewalld
setenforce 0

tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/

vim /etc/profile.d/java.sh

export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

source /etc/profile

2. tomcat服务配置
cd /opt
tar zxvf apache-tomcat-8.5.16.tar.gz
mkdir /usr/local/tomcat
mv apache-tomcat-8.5.16 /usr/local/tomcat/tomcat1
cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2

3.配置 tomcat 环境变量
vim /etc/profile.d/tomcat.sh

#tomcat1
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1

#tomcat2
export CATALINA_HOME2=/usr/local/tomcat/tomcat2
export CATALINA_BASE2=/usr/local/tomcat/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat/tomcat2

source /etc/profile

4.修改 tomcat2 中的 server.xml 文件,要求各 tomcat 实例配置不能有重复的端口号
vim /usr/local/tomcat/tomcat2/conf/server.xml
——————————————————————————————————————————————————————————————————————---
<Server port="8006" shutdown="SHUTDOWN">		#22行,修改Server prot,默认为8005 -> 修改为8006
<Connector port="8081" protocol="HTTP/1.1"		#69行,修改Connector port,HTTP/1.1  默认为8080 -> 修改为8081
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />	#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010

5.修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量
vim /usr/local/tomcat/tomcat1/bin/startup.sh 

# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1

vim /usr/local/tomcat/tomcat1/bin/shutdown.sh

# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1

vim /usr/local/tomcat/tomcat2/bin/startup.sh 
##添加以下内容
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2

vim /usr/local/tomcat/tomcat2/bin/shutdown.sh

# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2

6.启动各 tomcat 中的 /bin/startup.sh

/usr/local/tomcat/tomcat1/bin/startup.sh 
/usr/local/tomcat/tomcat2/bin/startup.sh 

netstat -natp | grep java

1.2.2 .30主机配置

1.配置java环境
systemctl stop firewalld
setenforce 0

tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/

vim /etc/profile.d/java.sh

export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

source /etc/profile
2. 安装tomcat服务
tar zxvf apache-tomcat-8.5.16.tar.gz

mv /opt/apache-tomcat-8.5.16/ /usr/local/tomcat

/usr/local/tomcat/bin/shutdown.sh 
/usr/local/tomcat/bin/startup.sh

netstat -ntap | grep 8080

1.3 动静分离配置

1.3.1 .25主机Tomcat配置

mkdir /usr/local/tomcat/tomcat1/webapps/test /usr/local/tomcat/tomcat2/webapps/test

vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>   #指定为 test1 页面
</head>
<body>
<% out.println("动态页面 1,From 192.168.80.25 8080 端口");%>
</body>
</html>

vim /usr/local/tomcat/tomcat1/conf/server.xml
————————————————————————————————————————148行,删除原有host配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" />
</Host>

/usr/local/tomcat/tomcat1/bin/shutdown.sh 
/usr/local/tomcat/tomcat1/bin/startup.sh 


vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>   #指定为 test2 页面
</head>
<body>
<% out.println("动态页面 2,From 192.168.80.25 8081 端口");%>
</body>
</html>

vim /usr/local/tomcat/tomcat2/conf/server.xml
————————————————————————————————————————148行,删除原有host配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" />
</Host>

/usr/local/tomcat/tomcat2/bin/shutdown.sh 
/usr/local/tomcat/tomcat2/bin/startup.sh 

1.3.2 .30主机Tomcat配置

mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp
——————————————————————————————————————————————编辑动态页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>   #指定为 test3 页面
</head>
<body>
<% out.println("动态页面 3,From 192.168.80.30);%>
</body>
</html>

vim /usr/local/tomcat/conf/server.xml
#由于主机名 name 配置都为 localhost,需要删除前面的 HOST 配置
————————————————————————————————————————————————148行
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true">
	</Context>
</Host>
——————————————————————————————————————————————————————————————————
#重启服务
/usr/local/tomcat/bin/shutdown.sh 
/usr/local/tomcat/bin/startup.sh 

1.3.3 nginx主机配置

echo '<html><body><h1>这是静态页面</h1></body></html>' > /usr/local/nginx/html/index.html

vim /usr/local/nginx/conf/nginx.conf

......
http {
......
	#gzip on;
	
	#配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
	upstream tomcat_server {
		server 192.168.80.25:8080 weight=1;
		server 192.168.80.25:8081 weight=1;
		server 192.168.80.30:8080 weight=1;
	}
	
	server {
		listen 80;
		server_name www.canyun.com;
	
		charset utf-8;
	
		#access_log logs/host.access.log main;
		
		#配置Nginx处理动态页面请求,将 .jsp文件请求转发到Tomcat 服务器处理
		location ~ .*\.jsp$ {
			proxy_pass http://tomcat_server;
#设置后端的Web服务器可以获取远程客户端的真实IP
##设定后端的Web服务器接收到的请求访问的主机名(域名或IP、端口),默认HOST的值为proxy_pass指令设置的主机名。如果反向代理服务器不重写该请求头的话,那么后端真实服务器在处理时会认为所有的请求都来在反向代理服务器,如果后端有防攻击策略的话,那么机器就被封掉了。
			proxy_set_header HOST $host;
##把$remote_addr赋值给X-Real-IP,来获取源IP
			proxy_set_header X-Real-IP $remote_addr;
##在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		}
		
		#配置Nginx处理静态图片请求
		location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
			root /usr/local/nginx/html/img;
			expires 10d;
		}
		
		location / {
			root html;
			index index.html index.htm;
		}
......
	}
......
}

2.操作截图

2.1 编译安装nginx

image
image

2.2部署Tomcat服务

.25主机,基于端口的虚拟主机配置
image
image
image
image
image
.30主机配置
image
image

2.3动静分离配置

2.3.1 .25主机配置

image
image
image

2.3.2 .30主机Tomcat配置

image
image

2.3.3 nginx服务配置

image
image

2.4 动静分离测试

静态页面
image
图片处理

image
动态页面测试
image

标签:CATALINA,虚拟主机,tomcat,nginx,Nginx,Tomcat,usr,export,local
来源: https://www.cnblogs.com/Canyun-blogs/p/16368933.html