小编n1c*_*1ck的帖子

逆向代理后面的Thymeleaf模板(在Spring启动应用程序中)没有正确形成url

当在反向代理后面的服务器上使用Thymeleaf时,我很难正确地形成相对URL(在我的例子中是nginx).

假设我在nginx中有以下位置:

location /app {
    proxy_pass http://10.0.0.0:8080;
}
Run Code Online (Sandbox Code Playgroud)

我有以下Thymeleaf 3页(index.html):

<html xmlns:th="http://www.thymeleaf.org">

<head>
    <link rel="stylesheet" th:href="@{/css/some.css}" />
</head>
<body>
    Hello world!
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

使用的服务器是一个嵌入式Tomcat服务器(Spring启动),在上下文中运行/.

当我发送请求时http://example.com/app,我将index.html页面作为响应.但是,无法检索css,因为当在Thymleaf模板中构造URL时,它使用tomcat服务器的上下文路径,即/.构建的url index.html看起来如下:

http://example.com/css/some.css
Run Code Online (Sandbox Code Playgroud)

这显然导致找不到404.URL需要形成如下:

http://example.com/app/css/some.css
Run Code Online (Sandbox Code Playgroud)

我需要配置什么才能让Thymeleaf形成URL http://example.com/app/css/some.css?我宁愿不对某些配置文件或其他类似的基本URL进行硬编码.我想我需要在nginx配置中添加一些内容,但我不确定究竟是什么.

proxy spring tomcat nginx thymeleaf

5
推荐指数
1
解决办法
761
查看次数

标签 统计

nginx ×1

proxy ×1

spring ×1

thymeleaf ×1

tomcat ×1