我的Play应用运行了:
http://localhost:9000
Nginx代理将其传递给此网址:
http://localhost/Demo/
我有静态资产的问题。例如,html模板中的此资产
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
将会
http://localhost/assets/stylesheets/main.css
显然导致无法找到。如果我将其更改为此(在网址前添加/Demo
):
<link rel="stylesheet" media="screen" href="/Demo@routes.Assets.at("stylesheets/main.css")">
现在它将正确转到此网址:
http://localhost/Demo/assets/stylesheets/main.css
我的问题是:如何将此/Demo
添加到我的所有静态资源中,而无需将其硬编码到我的模板中?我更喜欢使用Play的路由来解决这个问题,并限制对nginx conf进行的更改。
我已尝试在application.conf
中添加网址前缀
application.context="/Demo"
但这会影响所有网址,不仅仅是静态网址,因此不是解决方案。有什么想法吗?
我的筹码: Play Framework 2.2.1 / 斯卡拉2.10.3 / Java 8 64位/ Nginx 1.4.3 / Ubuntu 13.10 64bit
更新:解决方案感谢@biesior提供Java版本,我已将其转换为scala:
package core
import controllers.routes
object Assets {
def at(path: String): String = {
"/Demo" + routes.Assets.at(path)
}
}
答案 0 :(得分:3)
使用自己的资产实现(即在utils
包中)覆盖路径:
package utils;
import controllers.routes;
public class MyAssets {
public static String at(String path){
return "/Demo"+ routes.Assets.at(path).toString();
}
}
模板中的:
<img src='@utils.MyAssets.at("images/logo.png")' alt="">
另一方面,目标项目是否也在子目录中工作?如果没有,它只是更容易使用子域,也可以使用localhost,只需配置你的nginx用于该项目即:http://demo.loc
而不是http://localhost/Demo
并添加此'域'到您的hosts
文件