2

我有一个 Rails 网络应用程序,它在动态静态 url 下有静态文件。例如:

/projects/1/attachments/some_file.xls

我想设置 Nginx 以重定向到服务器上的静态文件:

/public/attachments/1/some_file.xls

其中 1 是动态项目 ID。

位置块和重写语句如何查找 Nginx 配置文件?


更新

我在下面标记了一个答案,因为它回答了我原来的问题。尽管在我的情况下不需要重写我的项目附件 url。我忘记了我已经在我的 Rails 视图 erb 中重新映射了 url。

我真正的目标是阻止 Thin 将缓存响应标头添加到我的附件文件中。最终,我可以通过添加如下附件的位置来使用 Nginx 来防止这种情况:

location /attachments/ {
    expires    off;
    add_header Pragma "no-cache";
    add_header Cache-Control "no-cache, no-store";
    access_log off; 
    break;
}
4

2 回答 2

0

这是一个简单的例子。

location /project/ {
    rewrite ^/project/([0-9]+)/attachments/(.*)$ /public/attachments/$1/$2 last;
}

如果需要更复杂的规则,请查看官方帮助。

http://wiki.nginx.org/HttpRewriteModule#rewrite

于 2012-08-08T18:38:42.027 回答
0

Sounds like you're doing something wrong with your image/file library, as you shouldn't have to do any nginx rewrite rules for Paperclip, Dragonfly, etc to just work. For example in Paperclip you can set the url and/or path format explicitly when you use the DSL in the Model. For Paperclip it would look like this:

has_attached_file :attachment,
                  :url => '/attachments/:id/:style/:basename.:extension',
                  :path => ':rails_root/public/attachments/:id/:style/:basename.:extension'

Then everything should get served by nginx automatically since it's in your public directory. No rewrite rule necessary.

于 2012-08-08T19:39:35.093 回答