一、背景
公司內網服務訪問通過國外nginx轉發的接口,報nginx跨域問題。
二、步驟
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,但是不加還是會報錯。