Nginx 跨域配置文件

项目重构中,我们需要进行前后端分离,而且前后端分别部署在不同的服务器,而且不通过 Nginx 做反向代理,所以,后端允许跨域势在必行,由于本项目采用SSO认证,而且是内网环境,所以只要在 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;
}

引用:
enable cross-origin resource sharing

你可能感兴趣的:(Nginx 跨域配置文件)