代理服务器 - 被 CORS 策略阻止:不存在 'Access-Control-Allow-Origin' header(第一个请求有效,第二个请求无效)

Proxy Server - Blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present (First request works, second request doesn't)

我有一个用于屏蔽域的代理服务器,它加载资产(当用户访问 server.com,通过 nginx,它实际上是指向 server.com/123123)。

http 请求有效,但是我遇到了 ajax 请求的问题。

在我的服务器 nginx (server.com) 中,我有这些 headers:

add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';

在我的 example.com nginx 中,我添加了:

proxy_set_header Host server.com;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

server {
   listen 443 ssl;
   listen [::]:443 ssl;

   server_name example.com;

   location = / {
       proxy_pass https://server.com/123123;
   }

   #Certs
   #ssl_certificate y.pem;
   #ssl_certificate_key x.pem;
}

我的后端是 Laravel 但是我不确定它是否与此有关。

我做错了什么,导致一个 POST 请求有效但另一个无效? /validate-order/{orderId} 应该 return 一个 json 那么这可能是原因吗?

显然 nginx 没有将 headers 添加到错误响应代码中;并且有一种方法可以强制它 add_header 总是

add_header 'Access-Control-Allow-Origin' * always;

为我服务器中的所有 headers 添加“always”关键字,成功了!

非常感谢akawhy's answer