Tengo un sitio web con la siguiente segmentación:
api.example.com 
developers.example.com 
example.com
Me gustaría permitir ambos example.comy developers.example.comhacer solicitudes AJAX api.example.com.
Mi configuración nginx hasta ahora api.example.com, que es una aplicación de Rack servida por unicornio, se ve así:
upstream app_server {
  server unix:/tmp/api.example.com.sock fail_timeout=0;
}
server {
       listen 80;
       server_name api.example.com;
       access_log /home/nginx/api.example.com/log/access.log;
       error_log /home/nginx/api.example.com/log/error.log;
       location / {
         add_header 'Access-Control-Allow-Origin' 'http://example.com,http://developers.example.com';
         add_header 'Access-Control-Allow-Credentials' 'true';
         add_header 'Access-Control-Allow-Headers' 'Content-Type,Accept';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $http_host;
         proxy_redirect off;
         proxy_pass http://app_server;
       }
}
Según mi lectura, esto debería ser suficiente para lo que estoy tratando de hacer.
La respuesta de OPCIONES :
HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Sat, 28 Apr 2012 17:20:08 GMT
Content-Type: application/json
Connection: close
Status: 200 OK
Content-Length: 0
Access-Control-Allow-Origin: http://developers.example.com,http://example.com
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type,Accept
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Pero cuando intento lo siguiente en la consola de Chrome:
$.ajax("http://api.example.com", {
  type: 'get',
  contentType: "application/json",
  accept: "application/json"
}).success(function(data){
  console.log("success!", data);
}).fail(function(jqxhr, statusText){
  console.log("fail!", jqxhr, statusText);
})
Veo:
XMLHttpRequest cannot load http://api.example.com/. Origin
http://developers.example.com is not allowed by Access-Control-Allow-Origin.
Y lo mismo para http://example.com .
¿Qué me estoy perdiendo?
Si configuro el Access-Control-Allow-Origina *entonces veo:
HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Sat, 28 Apr 2012 17:28:41 GMT
Content-Type: application/json
Connection: close
Status: 200 OK
Content-Length: 0
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type,Accept
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Pero la solicitud de jQuery todavía falla, con Chrome también destacando que el pre-vuelo OPTIONSfalló (a pesar de que regresó 200 OK).

Access-Control-Allow-Headerencabezado y modifiqué mi llamada a jQuery de esta manera: lo$.ajax("http://api.example.com", { type: 'get', crossDomain: true})que evitó queOPTIONSocurriera la verificación previa.Usando un
ifen unlocationbloque en una configuración nginx como esta:Hacer que nginx haga cosas raras. Específicamente,
proxy_passytry_filesno funciona como se esperaba. Ver http://wiki.nginx.org/IfIsEvil para más información.fuente