当我向 Rails Controller 操作调用发布请求时出现 405 错误。 post 请求用于获取 json 文件,而 get 请求传递标准的 html 页面。 问题是 html 被缓存了。
我在 http://millarian.com/programming/ruby-on-rails/nginx-405-not-allowed-error/ 上看到了这个问题的解决方案
if ($request_method != GET) {
proxy_pass http://foobar;
break;
}
proxy_pass 中的 url 通常是 mongrel 服务器的 url。乘客有类似的解决方案吗?
我是一个完整的 nginx 新手,正在尝试从 apache 迁移。
最佳答案
我终于找到了解决办法。 我有一组重写条件来处理我的 Rails 配置使用的缓存目录。
# Check the path + .html
if (-f $document_root/cache/$uri.html) {
rewrite (.*) /cache/$1.html break;
}
...
即使请求是 POST,也会应用这些重写,从而导致 405 错误。
我设法仅在请求为 GET 时应用重写。我不确定这是否是最有效的解决方案,但它似乎有效。学习如何在 Nginx 中处理多个条件是最棘手的部分。 (来源:http://wiki.nginx.org/RewriteMultiCondExample)
set $dest "";
if ($request_method = GET) {
set $dest "/cache";
}
# Check / files with index.html
if (-f $document_root/cache/$uri/index.html) {
set $dest $dest/$uri/index.html ;
}
# Check the path + .html
if (-f $document_root/cache/$uri.html) {
set $dest $dest/$uri.html ;
}
# Check directly
if (-f $document_root/cache/$uri) {
set $dest $dest/$uri ;
}
if ($dest ~* ^/cache/(.*)$) {
rewrite (.*) $dest break;
}
有更好的解决方案吗?
关于ruby-on-rails - Nginx + 乘客 +rails : Getting 405 error on post request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5424131/