크로스 원본 자원 공유 (CORS)를 지원하는 JSON API를 제공하는 NginX를 통해 호스팅되는 Rails3 앱이 있습니다. 관련 헤더는 Rails 앱이 아닌 NginX를 통해 추가해야합니다. Rails3 및 NginX CORS 헤더
그러므로 나는 OPTIONS 프리 플라이트 요청 가져 routes.rb에서 포수를 추가 :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 헤더가 추가됩니다. 사전
펠릭스
에서 는들으
감사합니다. – Dave