Nginx 跨域設置

web應用通常會碰到跨域的問題,特別是在將字體文件放在另一個域名下(cdn緩存)的時候會出現無法訪問的問題,瀏覽器會報如下錯誤警告:

Font from origin 'http://cdn.xxxx.com' has been blocked from loading by Cross-Origin Resource Sharing policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.xxxx.com' 
is therefore not allowed access.

意思是說 http://www.xxxx.com 不被 http://cdn.xxxx.com 服務跨域允許,需要在 nginx 的 cdn.xxxx.com服務的配置中加上跨域允許的配置:

         add_header Access-Control-Allow-Origin http://www.xxxx.com;
add_header Access-Control-Allow-Headers Origin,X-Requested-With,Content-Type,Accept;
add_header Access-Control-Allow-Methods POST,GET;
add_header Access-Control-Allow-Credentials true;

重啓nginx就能解決這個問題。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章