Rails3和NginX CORS标头

时间:2012-09-02 11:14:47

标签: ruby-on-rails nginx cors

我有一个通过NginX托管的Rails3应用程序,它提供了一个支持跨源资源共享(CORS)的JSON API。 相关的标题应该通过NginX添加,而不是通过Rails应用程序添加。

因此我在routes.rb中添加了一个catcher来获取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":"demo@fooapp.net", "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":"demo@fooapp.net", "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标头?

提前谢谢

菲利克斯

1 个答案:

答案 0 :(得分:2)

这有点晚了但是因为我遇到了同样的问题,所以可能会帮助其他人。

您需要在位置栏内启用乘客

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

我希望这会有所帮助。