在server或location中添加如下配置即可:
#允许跨域请求的域,* 代表所有
add_header 'Access-Control-Allow-Origin' * always;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true' always;
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' * always;
#允许请求的header
add_header 'Access-Control-Allow-Headers' * always;
if ($request_method = 'OPTIONS') {
return 204;
}
如图所示:
- 只针对当前location配置允许
- 如果配置到server下,则当前server下的location都生效