I'm having a 405 error when I call a post request to a rails controller a开发者_运维百科ction. The post request is used to get a json file wile the get request delivers a standard html page. The problem is that the html is cached.
I've seen a solution for this problem on http://millarian.com/programming/ruby-on-rails/nginx-405-not-allowed-error/
if ($request_method != GET) {
proxy_pass http://foobar;
break;
}
the url in proxy_pass is the normally the url for the mongrel server. Is there similar solution for passenger ?
I'm a complete nginx newbie, trying to migrate from apache.
I finally found a way around. I had a set of rewrite conditions to deal with the cache directory my configuration of Rails uses.
# Check the path + .html
if (-f $document_root/cache/$uri.html) {
rewrite (.*) /cache/$1.html break;
}
...
Those rewrites were applied even if the request was a POST, leading to the 405 error.
I managed to apply the rewrite only if the request is a GET. I'm not sure if it is the most efficient solution, but it seems to work. Learning how to deal with multiple conditions in Nginx was the trickiest part. (source : 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;
}
Any better solution?
精彩评论