Origin关闭时,Nginx缓存请求被CORS策略阻止



只要Origin Server在运行,我的缓存就可以正常工作。我使用"proxy_cache_use_stale",如下所示。尝试使用不同的错误代码和更新,但不会显示缓存的文件(它们在缓存文件夹中(。我还尝试了我发现的各种CORS解决方案,但也不起作用。

我的.conf文件:

proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=300d use_temp_path=off;
server {
listen 80;
listen [::]:80;
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Headers' "Origin, X-Requested-With, Content-Type, Accept" always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
proxy_cache my_cache;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504 http_403 http_404 http_429;
proxy_cache_background_update on;
proxy_cache_lock on;
proxy_cache_key $host$request_uri|$request_body;
proxy_cache_valid 200 302 301 800d;
proxy_ignore_headers Cache-Control;
proxy_cache_methods GET HEAD POST;
proxy_pass example.com;
add_header X-Cache-Status $upstream_cache_status;
}
}

错误:

Access to XMLHttpRequest at 'http://example.com' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

问题已解决。CORS没有问题,问题出在json中。

最新更新