nginx CORS 跨域问题

一、背景

公司内网服务访问通过国外nginx转发的接口,报nginx跨域问题。

nginx CORS 跨域问题_第1张图片

二、步骤

1.在nginx.conf添加如下配置。
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
2.在相应访问的域名配置文件添加如下配置。
 #在location处添加以下内容
 if ($request_method = 'OPTIONS') {
 return 200;
 }
3.或者直接在相应访问的域名配置文件添加如下配置。
add_header 'Access-Control-Allow-Origin' '*' always;
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;
}

三、问题

1.网上查了很多资料,说是nginx版本大于1.7,“add_header ‘Access-Control-Allow-Origin’ ‘*’ always”,always可以不加。我的版本是1.8,但是不加还是会报错。

你可能感兴趣的:(技术总结,个人学习)