Skip to content

Instantly share code, notes, and snippets.

@uicosp
Last active August 15, 2018 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uicosp/b632982153d81a009bb7a5d288cd20dc to your computer and use it in GitHub Desktop.
Save uicosp/b632982153d81a009bb7a5d288cd20dc to your computer and use it in GitHub Desktop.
Tengine 版跨域配置
# 跨域规范请查阅
# https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS
set $cors 'FALSE';
if ($http_origin ~ '^https?://(localhost|.*\.domain\.com)$') {
set $cors 'TRUE';
}
if ($request_method = 'OPTIONS') {
set $flag "${cors}+PREFLIGHT";
}
if ($request_method != 'OPTIONS') {
set $flag "${cors}+SIMPLE";
}
# 简单请求
if ($flag = 'TRUE+SIMPLE') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
# required to be able to read Authorization header in frontend
#add_header 'Access-Control-Expose-Headers' 'Authorization';
}
# 预检请求
if ($flag = 'TRUE+PREFLIGHT') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With';
# required to be able to read Authorization header in frontend
#add_header 'Access-Control-Expose-Headers' 'Authorization';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment