Skip to content

Instantly share code, notes, and snippets.

@Micjoyce
Last active January 27, 2019 13:55
Show Gist options
  • Save Micjoyce/c539d13b72606d7103c4df90f6fcab51 to your computer and use it in GitHub Desktop.
Save Micjoyce/c539d13b72606d7103c4df90f6fcab51 to your computer and use it in GitHub Desktop.
server {
listen 80;
server_name b.com
location /{
proxy_pass http://localhost:8080/
# 允许任何方法调用例如 OPTIONS GET POST PUT PATCH DELETE
add_header Access-Control-Allow-Methods: *;
# 跨域访问缓存时间限制,OPTIONS 预检查会被缓存,1小时均不会再重新请求
add_header Access-Control-Max-Age: 3600;
# 携带被调用方的 cookie
# 例如 a.com 跨域访问 b.com, 此时 a.com 请求 b.com 的 api 时会携带上 b.com 的 cookie
add_header Access-Control-Allow-Credentials true;
# 允许跨域的域名,如果需要携带 cookie 数据必须制定允许跨域域名,否则会失败
add_header Access-Control-Allow-Origin $http_origin;
# 允许跨域的自定义请求头
add_header Access-Control-Headers $http_access_control_request_headers;
# 如果是预检查,则直接返回 200
if ($request_method = OPTIONS){
return 200;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment