[英]Nginx Cache request blocked by CORS policy when Origin is down
只要源服務器正在運行,我的緩存就可以正常工作。 我使用“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 中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.