Estou movendo minhas APIs de um subdomínio para outro sem afetar os aplicativos já em execução. Eu tenho três servidores configurados no nginx, como:
Servidor de API original:
server {
listen 80;
server_name example.com;
root /var/www/example/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
add_header 'Access-Control-Allow-Origin' '*';
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~*/api/([a-zA-Z0-9_]+) {
proxy_pass http://127.0.0.1:4343/api/$1;
proxy_read_timeout 60s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header 'Access-Control-Allow-Origin' '*';
proxy_set_header 'Access-Control-Allow-Credentials' true;
}
...
}
Servidor transmitido por proxy:
server {
listen 4343;
server_name _;
root /var/www/exampleapi/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
add_header 'Access-Control-Allow-Origin' '*';
location / {
try_files $uri $uri/ /index.php?$args;
}
...
}
A chamada AJAX costumava funcionar perfeitamente nas APIs antigas, no entanto, para as novas, estou recebendo um erro no FF:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com/api/startup. (Reason: CORS header 'Access-Control-Allow-Origin' does not match '*, *').
E no Safari:
XMLHttpRequest cannot load https://example.com/api/startup. Origin https://myclient.com is not allowed by Access-Control-Allow-Origin.
Curling em shows novos e antigos de apis:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Como posso resolver este problema?