n1c*_*1ck 5 proxy spring tomcat nginx thymeleaf
当在反向代理后面的服务器上使用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配置中添加一些内容,但我不确定究竟是什么.
最终将 Tomcat 服务器的上下文与所需的 Nginx 位置进行匹配。根据上面的示例,上下文将设置为“/app”。
编辑:我在以下位置设置应用程序上下文属性application.yml:
server:
servlet:
contextPath: /app
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
761 次 |
| 最近记录: |