解决Nginx+Tomcat时ContextPath不同的问题

 

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

转载于:https://www.cnblogs.com/codestory/p/7716914.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Nginx中配置域名跳转,可以根据需要添加server节点到nginx.conf文件中。每个server节点对应一个域名,并设置相应的反向代理规则。例如,假设我们要将abc.com跳转到https://127.0.0.1:90,将sss.abc.com跳转到http://127.0.0.1:2223,可以按照以下方式进行配置: ``` server { listen 80; server_name abc.com; #你的域名 location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass https://127.0.0.1:90; #真正服务端口 } } server { listen 80; server_name sss.abc.com; #你的域名的二级域名 location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:2223; #真正服务端口 } } ``` 其中,每个server节点里的`server_name`指定了对应的域名,`location`指定了对应的请求路径规则,`proxy_pass`指定了真正服务的地址和端口。这样配置后,当用户访问abc.com,请求将被反向代理到https://127.0.0.1:90;当用户访问sss.abc.com,请求将被反向代理到http://127.0.0.1:2223。这样就实现了域名跳转。 请注意,域名解析是将域名指向网站空间的IP地址,而不是某个端口。因此,在Nginx配置中,需要指定具体的端口号来进行反向代理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Nginx实现域名跳转](https://blog.csdn.net/weixin_38236026/article/details/99688909)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值