Nginx 配置 CORS 跨域請求

剛好有客戶有跨網域訪問的需求,搜尋 Google 在 Nginx 配置相關 header 予以解決,把這個設定文件記錄一下。

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type';

#在 location 處增加以下内容

if ($request_method = 'OPTIONS') {
    return 200;
}

想深入研究跨網域原理,請參考這一篇文章:

https://www.imqianduan.com/nginx/cors.html

Written By Human

發佈留言

Your Mastodon Instance