自定义 NGINX mime 类型 URL 重写
我正在使用 Rails 3.1 和 NGINX。
我对网站所做的就是使用 URL hashbang + popstate 技巧来重新加载 URL,然后使用 AJAX 下载下一页。然后 Rails 会识别出这是一个 AJAX 请求,然后交付没有布局的视图(或精简版本)。这是可行的,但是当我尝试将输出缓存到我自己的扩展(称为 plain_html)中时,NGINX 不允许 URL 重写工作。
我正在将 Rails 页面缓存文件缓存到公共文件夹上的缓存目录中。这些是我对 HTML 文件的 URL 重写。
if (-f $document_root/cache/$uri/index.html) {
rewrite (.*) /cache/$1/index.html break;
}
if (-f $document_root/cache/$uri.html) {
rewrite (.*) /cache/$1.html break;
}
if (-f $document_root/cache/$uri) {
rewrite (.*) /cache/$1 break;
}
前两个适用于没有 HTML 扩展名的任何内容,最后一个适用于缓存文件夹中的所有内容。最后一个基本上应该适用于具有不同扩展名并且在缓存文件夹中找到的任何内容,例如:
GET /articles # -> /cache/articles.html
GET /articles.html # -> /cache/articles.html
GET /articles.plain_html # -> /cache/articles.plain_html
这似乎不适用于最终选项。它强制浏览器下载它(如果我在地址栏中访问它)并且下载文件的内容不是缓存内容(我尝试将单词直接添加到缓存文件中)。
我已经配置 NGINX 将其添加为 mime_type 并且我确信 mime 类型确实可以工作,因为当我这样做时:
GET /cache/articles.plain_html # -> this works fine
它可以直接工作。所以重写正在进行中。关于我应该做什么有什么想法吗?
顺便说一句,这种 plain_html 重写技术使用 Apache 效果很好。
I'm using Rails 3.1 and NGINX.
What I do with my websites is I use a URL hashbang + popstate trick to reload the URL while then using AJAX to download the next page. Rails then recognizes that this is a AJAX request and then delivers the view without the layout (or a stripped down version). This works, but when I try to cache the output into my own extension (called plain_html) then NGINX doesn't allow the URL rewriting to work.
I'm caching my Rails page cached files into a cache directory located on the public folder. These are my URL rewrites for my HTML files.
if (-f $document_root/cache/$uri/index.html) {
rewrite (.*) /cache/$1/index.html break;
}
if (-f $document_root/cache/$uri.html) {
rewrite (.*) /cache/$1.html break;
}
if (-f $document_root/cache/$uri) {
rewrite (.*) /cache/$1 break;
}
The first two are for anything that is without a HTML extension and the final one is a catch all for anything in the cache folder. The last one should basically work for anything that has a different extension and that is found within the cache folder like:
GET /articles # -> /cache/articles.html
GET /articles.html # -> /cache/articles.html
GET /articles.plain_html # -> /cache/articles.plain_html
This does not seem to work for the final option. It forces the browser to download it (if I access it in the address bar) and the contents of the downloaded file are not the cached contents (I tried adding words directly into the cache file).
I've configured NGINX to add this as a mime_type and I'm sure that the mime type does work because when I do:
GET /cache/articles.plain_html # -> this works fine
It works directly. So something is going on with the rewriting. Any ideas as to what I should do?
BTW this plain_html rewriting technique works fine using Apache.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上并不需要为此目的进行重写。
try_files /cache/$uri/index.html /cache/$uri.html /cache/$uri
应该可以解决问题。
http://wiki.nginx.org/HttpCoreModule#try_files
这可能会解决类型问题。
否则,有
default_type
指令来帮助 nginx 处理未知类型。http://wiki.nginx.org/HttpCoreModule#default_type
You don't really need a rewrite for the purpose.
try_files /cache/$uri/index.html /cache/$uri.html /cache/$uri
should do the trick.
http://wiki.nginx.org/HttpCoreModule#try_files
That may solve the type problem.
Otherwise there's
default_type
directive to help out the nginx with unknown types.http://wiki.nginx.org/HttpCoreModule#default_type
解决方案是在 NGINX 中为
plain_html
文件注册 mime-type 或将 default_type 设置为 content/html。The solution is to register a mime-type within NGINX for the
plain_html
file or set the default_type to content/html.