2012-09-02 49 views
3

我有一個通過NginX託管的Rails3應用程序,它提供了一個支持跨源資源共享(CORS)的JSON API。 相關標題應該通過NginX添加,而不是通過Rails應用程序。Rails3和NginX CORS頭文件

因此,我在routes.rb中添加了一個接球手去取OPTIONS預檢要求:

match "*options", controller: "application", action: "options", constraints: { method: "OPTIONS" } 

,我添加了相應的端點,以我的應用程序控制器:

def options 
    render :text => "", :layout => false 
end 

作品。現在,我要爲我的Nginx一個敞開CORS配置:

server { 
      listen 80; 
      server_name localhost; 
      passenger_enabled on; 
      root /home/deployer/apps/fooapp/current/public; 

      location /v1 { 
        if ($request_method = 'OPTIONS') { 
          add_header 'Access-Control-Allow-Origin' '*'; 
          add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
          add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-FooApp-Auth-Token'; 
          add_header 'Access-Control-Max-Age' 1728000; 
          add_header 'Content-Type' 'text/plain charset=UTF-8'; 
          add_header 'Content-Length' 0; 
          return 200; 
        } 
        if ($request_method = 'POST') { 
          add_header 'Access-Control-Allow-Origin' '*'; 
          add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
          add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-FooApp-Auth-Token'; 
        } 
        if ($request_method = 'GET') { 
          add_header 'Access-Control-Allow-Origin' '*'; 
          add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
          add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-FooApp-Auth-Token'; 
        } 
      } 
    } 

大,OPTIONS請求都在現在的響應所需的標題:

curl -i -H'Accept: application/json' -H'Content-Type: application/json' -XOPTIONS 'http://api.fooapp.net/v1/api_session' -d'{"user":{"email":"[email protected]", "password":"somepassword"}}' 

HTTP/1.1 200 OK 
Server: nginx/1.2.3 
Date: Sun, 02 Sep 2012 11:04:28 GMT 
Content-Type: application/octet-stream 
Content-Length: 0 
Connection: keep-alive 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Methods: GET, POST, OPTIONS 
Access-Control-Allow-Headers: DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-FooApp-Auth-Token 
Access-Control-Max-Age: 1728000 
Content-Type: text/plain charset=UTF-8 
Content-Length: 0 

但是例如一個真正的POST請求現在失敗404:

curl -i -H'Accept: application/json' -H'Content-Type: application/json' -XPOST 'http://api.fooapp.net/v1/api_session' -d'{"user":{"email":"[email protected]", "password":"somepassword"}}' 

HTTP/1.1 404 Not Found 
Server: nginx/1.2.3 
Date: Sun, 02 Sep 2012 11:06:19 GMT 
Content-Type: text/html 
Content-Length: 168 
Connection: keep-alive 

<html> 
<head><title>404 Not Found</title></head> 
<body bgcolor="white"> 
<center><h1>404 Not Found</h1></center> 
<hr><center>nginx/1.2.3</center> 
</body> 
</html> 

而且它有,引起Nginx的日誌說:

2012/09/02 13:06:19 [error] 2464#0: *3 open() "/home/deployer/apps/fooapp/current/public/v1/api_session" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "POST /v1/api_session HTTP/1.1", host: "api.fooapp.net" 

我是NginX的新手,我知道這是一個開放的以及非常輕量級的配置。我不明白爲什麼NginX試圖通過公共路由這些請求,以便不需要底層的Rails應用程序。我該如何改變配置,以便我的所有請求都像以前一樣傳遞給應用程序,但CORS頭文件仍然被添加?

提前

菲利克斯

THX

回答

2

這來得晚了一點,但因爲我只是碰到了同樣的問題,它可能會幫助別人。

您需要啓用位置塊

location /{ 
    ... 
    passenger_enabled on; 
    ... 
} 

我希望這有助於內部的乘客。

+0

謝謝,,,加入它爲我工作 – Dave