一、nginx+tomcat主机实现
1.主机规划
nginx主机 | 172.18.12.20 |
tomcat主机 | 172.18.12.21 |
2.安装tomcat主机并部署一个简单的测试页面
# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel # vim /etc/profile.d/java.sh
JAVA_HOME=/usr
export JAVA_HOME
# . /etc/profile.d/java.sh
3.安装tomcat
# yum install -y tomcat tomcat-lib tomcat-webapps tomcat-admin-webapps
4..部署一个测试页面
# mkdir /var/lib/tomcat/webapps/myapp # mkdir /var/lib/tomcat/webapps/myapp/{classes,lib,WEB-INF,META-INF} # mkdir /var/lib/tomcat/webapps/myapp/index.jsp # vim /var/lib/tomcat/webapps/myapp/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
5.启动tomcat
# systemctl start tomcat
7.nginx主机安装配置
# yum install -y nginx # vim /etc/nginx/nginx.conf
location ~* \.(jsp|do) {
proxy_pass http://172.18.12.21:8080;
}
# nginx -t # nginx
二、Apache+tomcat主机实现
主机规划
Apache主机 | 172.18.12.20 |
TomcatA主机 | 172.18.12.21 |
为了不让nginx主机影响我们的实验,我们先卸载nginx,再安装Apache
#yum remove nginx # yum install -y httpd
Apache反代后端主机有三种实现我们分开来一一演示,注意此处我们都是基于虚拟主机实现
2.注释中心主机
# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
3.第一种实现方式
使用httpd自带代理模块proxy_http_module 基于http协议来实现
编写httpd的配置文件
# vim /etc/httpd/conf.d/http_proxy.conf
<VirtualHost *:80>
ServerName dy.magedu.com
ProxyRequests Off 关闭正向代理
ProxyVia On 指明是从哪个主机代理指后端主机的
ProxyPreserveHost On 关闭其他重定向的主机名
<Proxy *>
Require all granted 授权代理所有的访问请求
</Proxy>
ProxyPass / http://172.18.12.21:8080/
ProxyPassReverse / http://172.18.12.21:8080/
<Location />
Require all granted
</Location>
</VirtualHost>
测试语法并重载httpd令配置文件生效
#httpd -t #systemctl reload httpd
第二种方法使用httpd自带的模块 proxy_ajp_module 来实现
此种实现与第一种的区别在于所使用的协议不同,我们可以利用上一个配置文件只是修改协议和端口即可以实现
# mv /etc/httpd/conf.d/http_proxy.conf /etc/httpd/conf.d/ajp_proxy.conf
<VirtualHost *:80>
ServerName dy.magedu.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://172.18.12.21:8009/
ProxyPassReverse / ajp://172.18.12.21:8009/
<Location />
Require all granted
</Location>
</VirtualHost>
第三种使用第三方模块mod_jk实现
为防止原来的配置文件影响我们的实验将原来的配置文件重命名
# mv /etc/httpd/conf.d/ajp_proxy.conf{,.bak}
1.编译安装mod_jk
# yum groupinstall -y "development tools" "server platform development" # yum install httpd-devel # tar -xf tomcat-connectors-1.2.40-src.tar.gz # cd tomcat-connectors-1.2.40-src/native/ # ./configure --with-apxs=/usr/bin/apxs #make && make install
2.提供mod_jk的配置文件
# vim /etc/httpd/conf.d/mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* TomcatA
JkMount /jk_status StatA
# vim /etc/httpd/conf.d/workers.properties
worker.list=TomcatA,StatA
worker.TomcatA.host=172.18.12.21
worker.TomcatA.port=8009
worker.TomcatA.type=ajp13
worker.StatA.type=status
#httpd -t #systemctl restart httpd
本文出自 “11243407” 博客,请务必保留此出处http://11253407.blog.51cto.com/11243407/1787990
常见的nginx,apache反代用户请求至tomcat主机的实现方法
原文地址:http://11253407.blog.51cto.com/11243407/1787990