【问题标题】:Nginx + passenger +rails : Getting 405 error on post requestNginx + 乘客 +rails:发布请求时出现 405 错误
【发布时间】:2011-03-24 18:59:41
【问题描述】:

当我向 Rails 控制器操作调用发布请求时出现 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 迁移。

【问题讨论】:

  • 您实际上是在使用 POST 请求发布数据,还是只是使用它来区分 JSON 和 HTML 请求?如果是这样,有更好的方法来完成你正在做的事情。
  • 我希望 rails 提供 json resquet 而不是 nginx 返回 405 错误。
  • 是的,我正在发送一个帖子请求。当我关闭页面缓存时,它运行良好。
  • 您是否尝试过将该行添加到您的 Nginx 配置文件中?如果它已经在 Mongrel 中工作,你不应该改变任何东西来让它在乘客中工作。
  • 不,我没有,因为我不知道乘客使用什么网址。 Mongrel 使用特定的端口。

标签: ruby-on-rails nginx passenger


【解决方案1】:

我终于找到了解决办法。 我有一组重写条件来处理我的 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;
}

有更好的解决方案吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-05
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多