解决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
    评论
在Windows系统中,配置NginxTomcat项目需要进行以下详细配置。 首先,要确保已经正确安装并配置好NginxTomcat。然后,在Nginx的配置文件(一般为nginx.conf)中,需要进行如下配置: 1. 配置NginxTomcat的反向代理关系。可以在Nginx的http部分中加入如下代码: ``` location / { proxy_pass http://localhost:8080; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } ``` 这里将Nginx的80端口与Tomcat的8080端口建立反向代理关系。 2. 配置虚拟主机。如果需要配置多个Tomcat项目,可以通过虚拟主机来实现。在Nginx的http部分中添加如下代码: ``` server { listen 80; server_name your_domain_name; location / { proxy_pass http://localhost:8080; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } ``` 这里的"your_domain_name"替换为你的域名,配置不同的域名可以区分不同Tomcat项目。 接下来,需要配置Tomcat的服务器。在Tomcat的server.xml中,进行如下配置: 1. 配置连接器。添加如下代码: ``` <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> ``` 这里的端口号可以根据需要进行调整。 2. 配置虚拟主机。如果需要配置多个Tomcat项目,可以通过虚拟主机来实现。添加如下代码: ``` <Host name="your_domain_name" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="your_application_path" /> </Host> ``` 这里的"your_domain_name"替换为你的域名,"your_application_path"替换为你的项目路径。 配置完成后,保存文件并重新启动NginxTomcat。现在,Nginx将可以通过80端口访问Tomcat项目,而Tomcat将通过8080端口提供服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值