項目重構(gòu)中,我們需要進(jìn)行前后端分離币喧,而且前后端分別部署在不同的服務(wù)器,而且不通過 Nginx 做反向代理袱耽,所以杀餐,后端允許跨域勢在必行,由于本項目采用SSO認(rèn)證朱巨,而且是內(nèi)網(wǎng)環(huán)境史翘,所以只要在 nginx 配置文件中,加入允許跨域頭冀续,這樣既可避免改動代碼琼讽。
file:cors.conf
# 加入跨域
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,x_requested_with';
if ($request_method = 'OPTIONS') {
return 204;
}