2011-03-24 45 views
2

我在向rails控制器操作發送post請求時遇到了405錯誤。 發佈請求用於獲取json文件,只要get請求提供標準html頁面。 問題是,HTML被緩存。Nginx + passenger + rails:在發佈請求時獲取405錯誤

我已經看到了這個問題的解決方案上http://millarian.com/programming/ruby-on-rails/nginx-405-not-allowed-error/

if ($request_method != GET) { 
    proxy_pass http://foobar; 
    break; 
} 

在proxy_pass的URL是針對雜種服務器正常的URL。乘客是否有類似的解決方案?

我是一個完整的nginx新手,試圖從apache遷移。

+0

你實際上是用POST請求發佈數據還是僅僅用它來區分JSON和HTML請求?如果是這樣,有一個更好的方法來完成你正在做的事情。 – 2011-03-24 19:25:23

+0

我想讓rails服務於json resquet而不是nginx返回405錯誤。 – jlfenaux 2011-03-24 19:36:29

+0

是的,我正在發送一個帖子請求。當我關閉頁面緩存時,它運行良好。 – jlfenaux 2011-03-24 19:45:11

回答

2

我終於找到了一個方法。 我有一套重寫條件來處理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; 
} 

有沒有更好的解決方案?