nginx CORS 跨域問題

一、背景

公司內網服務訪問通過國外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,但是不加還是會報錯。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章