Apache反向代理Tomcat
Apache反向代理Tomcat
环境
os:Windows 10
Apache:2.4.29
Tomcat:8.0.30
Apache下载
Windows版本下载地址:https://www.apachehaus.com/cgi-bin/download.plx
Apache安装
下载完成后,解压到自定义路径下即可。
目录结构:
Apache配置
httpd-vhosts.conf配置
在Apache安装路径下的conf/extra/httpd-vhosts.conf配置文件中添加下面内容:
<VirtualHost 192.168.6.251:80> #监听的服务器和端口
ServerAdmin aaa@qq.com
DocumentRoot "C:/Users/Admin/Desktop" #静态资源目录
ServerName localhost #域名
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
ProxyPreserveHost On
ProxyPass /ds-apache-web/ http://192.168.6.251:8082/ds-apache-web/ #反向代理的地址
ProxyPassReverse /ds-apache-web/ http://192.168.6.251:8082/ds-apache-web/ #反向代理的地址
</VirtualHost>
httpd.conf配置
在Apache安装路径下的conf/httpd.conf配置文件中,把下面内容前面的注释去掉即可:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Include conf/extra/httpd-vhosts.conf
Listen 80 #Apache监听的端口
ServerName localhost:80 #Apache服务地址和端口
启动Apache
打开cmd,切换到apache安装路径下的bin目录中,执行httpd.exe文件即可。
测试代码
a)新建maven项目;
b)编写控制器;
package com.dscomm.apache.ctrl;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/index")
public class IndexController {
@RequestMapping("/toIndex")
public void toIndex(HttpServletResponse response) throws IOException {
response.sendRedirect("https://www.baidu.com");
}
}
c)配置web.xml;
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- mvc servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 需要根据参数获取配置文件,配置处理器、适配器 (如果不配置,则默认加载 /WEB-INF/servlet-name+"-"+"servlet.xml") -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/spring-mvc.xml</param-value>
</init-param>
<!-- 表示启动容器时初始化该Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- *.action 使用以action结尾的请求由DispatcherServlet来解析 -->
<!-- / 所有访问地址都由DispatcherServlet来解析 。对于静态文件的访问需要配置不让DispatcherServlet解析。此方式可以实现rest -->
<!-- /* 会默认找到一个jsp页面,导致不去访问DispatcherServlet,导致解析出错 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- filter -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- <welcome-file-list>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> -->
<!-- http error status code exception 处理页面 -->
<error-page>
<error-code>403</error-code>
<location>/resources/page/page_403.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/resources/page/page_404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/resources/page/page_500.html</location>
</error-page>
</web-app>
d)在resources目录下添加spring/spring-ctrl-apache.xml配置文件;
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
default-lazy-init="true">
<!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 -->
<context:component-scan base-package="com.dscomm.apache.ctrl">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
</beans>
e)在resources目录下添加spring/spring-mvc.xml配置文件;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 导入需要扫描的配置 -->
<import resource="classpath*:**/spring/spring-ctrl-*.xml" />
<!-- 静态资源访问配置 -->
<!-- <mvc:resources location="/dist/" mapping="/dist/**" />
<mvc:resources location="/resources/" mapping="/resources/**" /> -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 -->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
f)配置pom.xml;
引入servlet jar和spring基本jar包即可。
测试代理
访问监听的服务器和端口,上面配置的是192.168.6.251:80;
访问地址:http://192.168.6.251:80/ds-apache-web/index/toIndex
测试结果:该请求会重定向到百度。
说明:该请求地址会被代理成http://192.168.6.251:8082/ds-apache-web/index/toIndex,然后IndexController控制器又重定向到https://www.baidu.com。
说明
对Apache配置项不理解的可以参考下面的三篇博客。
参考博客
《apache配置动静分离,允许跨域,并在反向代理的情况下维持默认主页》
《apache ProxyPass ProxyPassReverse概述》
上一篇: H5--01新增的语义化标签、form表单标签属性的增强
下一篇: 前端基础系列(二)HTML基础下