1        问题描述

项目前端模板使用Thymeleaf,在对各种URL进行格式化输出时,都使用@{uri}代码。它会自动读取项目部署的虚拟路径,添加到URI的前端输出。

真实测试和生产环境中,我们使用nginx+Tomcat的部署模式,这就会部署带来一个限制:ngxin配置proxy时,需要同后端application使用相同的context path。

一个比较典型的测试场景:同一个Tomcat,部署多个应用;同一个nginx,配置这三个应用的proxy,但要求都使用独立域名进行访问,不能添加 context path。如图:

2        Thymeleaf实现原理

仔细读Thymeleaf的源码,它对uri的封装,是通过 LinkBuilder类实现的。在SpringBoot项目中,相关的代码。

有几点需要注意:

1、              最终是在 StandardLinkBuilder中调用request.getContextPath()获取部署context.

2、              SpringTemplateEngine的构造函数中,直接new StandardLinkBuilder对象。

3、              ThymeleafAutoConfiguration的代码和相应的配置定义中,没有发现对LinkBuilder的配置参数。

3        解决方案

根据项目情况,可以有几个解决方案可供选择。

3.1   Filter + HttpServletRequestWrapper

思路:最终代码使用request.getContextPath(),我们只要重新封装一下Request,重写getContextPath()方法即可。

并在项目中添加一个Filer,核心代码为:

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain filterChain) throws IOException, ServletException {

CustomContextPathRequest requestWrapper =

new CustomContextPathRequest( (HttpServletRequest) request, this.contextPath);

filterChain.doFilter(requestWrapper, response);

}

3.2   扩展 AutoConfiguration

咱重点介绍一下这个方法,借此机会熟悉SpringBoot的机制。

思路:SpringBoot缺省的AutoConfiguration没有提供配置LinkBuilder,我们自己实现一个AutoConfiguration,在Spring完成SpringTemplateEngine成功之后,再替换器LinkBuilder实现。

3.2.1  ManualContextLinkBuilder

例子是将context path写死为 /demo ,实际代码中,可以通过在application.propertis中的变量来实现,并配合maven profile,实现不同运行环境的差异化实现。

public class ManualContextLinkBuilder extends StandardLinkBuilder {

private String nginxContextPath = “/demo”;

@Override

protected String computeContextPath(final IExpressionContext context,

final String base, final Map<String, Object> parameters) {

return nginxContextPath;

}

}

3.2.2  ManualContextLinkBuilderConfiguration

@Configuration

@AutoConfigureAfter(WebMvcAutoConfiguration.class)

public class ManualContextLinkBuilderConfiguration {

@Autowired

SpringTemplateEngine springTemplateEngine;

@Bean

public ILinkBuilder linkBuilder() {

ILinkBuilder linkBuilder = new ManualContextLinkBuilder();

springTemplateEngine.setLinkBuilder(linkBuilder);

return linkBuilder;

}

}

3.2.3  META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

tech.codestory.ManualContextLinkBuilderConfiguration

解决Nginx+Tomcat时ContextPath不同的问题的更多相关文章

  1. 【nginx】解决Nginx重启时提示nginx: [emerg] bind() to 0.0.0.0:80错误

    Nginx是一款轻量级的Web服务器,特点是占有内存少,并发能力强,因而使用比较广泛,蜗牛今天在一个VPS上重启Nginx时提示“nginx: [emerg] bind() to 0.0.0.0:80 ...

  2. 解决Nginx重启时提示nginx: [emerg] bind() to 0.0.0.0:80错误

    Nginx是一款轻量级的Web服务器,特点是占有内存少,并发能力强,因而使用比较广泛,蜗牛今天在一个VPS上重启Nginx时提示“nginx: [emerg] bind() to 0.0.0.0:80 ...

  3. 解决启动Tomcat时遇到INFO: Destroying ProtocolHandler ["ajp-apr-8009"]

    问题描述: 启动Tomcat时,出现INFO: Destroying ProtocolHandler ["ajp-apr-8009"]等信息 这说明端口号被占用了... 解决方法: ...

  4. 解决nginx启动时域名解析失败而导致服务启动失败的问题

    问题: nginx启动或者reload的时候,会对proxy_pass后面的域名进行DNS解析,如果解析失败,启动就会失败或者reload失败. 我们是to B的产品,客户的环境可能是不通公网的,因此 ...

  5. 解决Nginx+Tomcat下客户端https请求跳转成http的问题

    Nginx上开启https,  后端使用Tomcat,  两者间走http协议, 但发现如果tomcat应用存在跳转时, 则客户端浏览器会出现400 Bad Request的错误, 通过抓包发现原因是 ...

  6. 解决Nginx+Tomcat中https转http请求问题---解决js加载使用http的问题

    解决js加载使用http的问题 控制台错误提示: Mixed Content: The page at '' was loaded over HTTPS, but requested an insec ...

  7. Nginx+tomcat负载均衡时静态页面报404

    百度到的问题解决BLOG http://os.51cto.com/art/201204/326843.htm nginx+2台tomcat负载均衡,应用程序已部署,单独访问tomcat时,可以访问到所 ...

  8. 解决eclipse配置Tomcat时找不到server选项(Mac通用)

    集成Eclipse和Tomcat时找不到server选项: 按照网上的步骤如下: 在Eclipse中,窗口(window)——首选项(preferences)——服务器(Server)——运行时环境( ...

  9. [转]Eclipse启动Tomcat时45秒超时解决方法

    原文地址:http://it.oyksoft.com/post/6577/ Eclipse启动Tomcat时,默认配置的启动超时时长为45秒.假若项目启动超过45秒将会报错.两种解决方法:1.改XML ...

随机推荐

  1. 201521123069 《Java程序设计》 第12周学习总结

    1.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2.书面作业 Q1.将Student对象(属性:int id, String name,int age,doub ...

  2. 201521123002《Java程序设计》第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...

  3. java课程设计---彩票销售管理系统

    彩票购买销售系统 1.项目git地址 https://git.oschina.net/fenm/lotterry.git 部分项目地址提交截图 项目主要功能图 团队博客链接 http://www.cn ...

  4. 201521123014 《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  5. expect实现scp/ssh-copy-id非交互

    expect工具可以实现自动应答,从而达到非交互的目的. expect具体使用用法比较复杂,中文手册我正在翻译中,以后翻译完了做了整理再补.本文只有几个ssh相关最可能用上的示例. yum -y in ...

  6. Laravel 源码解读系列第四篇-Auth 机制

    前言 Laravel有一个神器: php artisan make:auth 能够快速的帮我们完成一套注册和登录的认证机制,但是这套机制具体的是怎么跑起来的呢?我们不妨来一起看看他的源码.不过在这篇文 ...

  7. session get和load方法对比

    get测试代码如下: public class Test { public static void main(String[] args) { // TODO Auto-generated metho ...

  8. 浅谈Linux虚拟内存

    我的orangepi内存很少,所以我打算给它弄个虚拟内存 首先建立一个1G的空文件: dd if=/dev/zero of=/home/swapfile bs=64M count=16 格式化为swa ...

  9. 图文详解在Windows server 2008 R2上安装SQL Server 2012集群

    1.准备: 4台服务器(1台AD.2台SQL服务器.1台iSCSI存储服务器) 9个IP(1个AD的IP.2个SQL服务器的IP.2个心跳IP.1个iSCSI存储服务器的IP.1个集群IP.1个DTC ...

  10. HDU2688-Rotate

    Recently yifenfei face such a problem that give you millions of positive integers,tell how many pair ...