[英]Rails asset pipeline and serving static files from nginx
I'm trying to access a file logo.png from my rails app.我正在尝试从我的 rails 应用程序访问文件 logo.png。 During the deploy process everything in /app/assets gets compiled and placed into /public/assets.在部署过程中,/app/assets 中的所有内容都被编译并放入/public/assets。 For example my file which had been named logo.png looks like this:例如,我名为 logo.png 的文件如下所示:
/public/assets/logo-e66eddecdb08ac2b7fe349da2a065d87.png
When I try to access that full filename nginx successfully serves it up directly because of these lines in my /etc/sites-enabled/myapp:当我尝试访问该完整文件名时,由于 /etc/sites-enabled/myapp 中的这些行,nginx 成功地直接提供了它:
try_files $uri/index.html $uri @unicorn;
root /home/deployer/apps/myapp/current/public
So I can browse to myserverhost.com/assets/logo-e66eddecdb08ac2b7fe349da2a065d87.png and it loads fine.所以我可以浏览到 myserverhost.com/assets/logo-e66eddecdb08ac2b7fe349da2a065d87.png 并且它加载正常。 However of course in my app I have that file referenced simply as "logo.png" and when I load my home page I'm getting a 404 not found on the logo.png.但是,当然在我的应用程序中,我将该文件简称为“logo.png”,当我加载主页时,我在 logo.png 上找不到 404。
How is nginx supposed to know that the filename has been mashed up with a hash at the end? nginx 应该如何知道文件名最后已与散列混合在一起?
EDIT: I reference the logo file like this in my scss file:编辑:我在我的 scss 文件中引用了这样的徽标文件:
#sidebar .logo-div {
border-bottom: 2px solid#ddd;
width:100%;
height:80px;
text-align:center;
background-image:url('logo.png');
background-position:center;
background-repeat:no-repeat;
margin-bottom:20px;
}
You'll have to use an url helper.您必须使用 url 助手。
background-image: url(image-path("logo.png"));
or asset-url("logo.png", image)
或asset-url("logo.png", image)
Rails automatically adds the hash in production. Rails 会在生产环境中自动添加哈希。
If your file is a css
file:如果您的文件是css
文件:
background-image: url(/assets/logo.png);
If your file is a sass
file can use the helper:如果您的文件是sass
文件,则可以使用帮助程序:
background-image: image-url("logo.png");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.