问题描述
我有一个生成大量图像的 Rails 应用程序;即具有显示操作的图像控制器从 SAN 获取图像并提供它.我有一个 caches_page,它可靠地将文件副本保存在自定义位置;RAILS_ROOT +/public/cache.文件在那里,看起来不错.
I have a Rails app which generates a lot of images; ie an images controller with a show action fetches an image from a SAN and serves it. I have a caches_page on this which reliably saves a copy of the file in a custom location; RAILS_ROOT + /public/cache. The files are there and look fine.
我正在通过 nginx 为应用程序提供服务,它似乎运行良好,但在我的一生中,我无法让它发送那些缓存的图像文件而不是执行 rails 操作.我对 nginx 很陌生,并且已经研究了一段时间,但不管我尝试什么都行不通 - 我做的事情根本上是错误的.
I am serving the app through nginx and it seems to work fine but for the life of me I can't get it to send those cached image files instead of hitting the rails action. I'm pretty new to nginx and have been researching it for some time but not matter what I try it doesn't work - I'm doing something fundamentally wrong.
澄清一下,我有一个这样的uri:
To clarify, I have a uri like this:
/images/show/123.jpg
文件可能存在于此处:
/cache/images/show/123.jpg
如何让 nginx 提供该文件而不传递请求?
How can I make nginx serve that file and not pass on the request?
这是我尝试过的不完整列表:
Here's an incomplete list of what I've tried:
if (-f /cache$request_filename) {
rewrite (.*) /cache$1 break;
break;
}
if (-f /cache$uri.jpg) {
rewrite (.*) /cache$1.jpg break;
}
if (-f /cache/images/show/$request_filename) {
rewrite (.*) /cache/images/show/$1.jpg break;
}
有什么想法吗?
更新:我回答了我自己的问题.正确的规则是:
update: I answered my own question. The correct rule was:
if (-f $document_root/cache$request_uri) {
rewrite (.*) /cache$1 break;
}
推荐答案
nginx 的推荐方式是使用try_files"指令
the recommended way for nginx is usingthe "try_files" directive
类似的东西
location /images/show {
root ...;
try_files $document_root/cache$uri /fromrootpath/$uri;
}
这篇关于配置 nginx 以提供缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!