开发需要一个满足正则匹配的跨域配置,就是指 只有满足正则匹配的域名($http_origin)才可以允许跨域
参考配置
# map需要定义在Server块外面
map $http_origin $allow_origin_xxx {
~^https?://(.*\.)?airdrop.domain.com(:\d+)?$ $http_origin;
~^https?://(.*\.)?dev.domainddd.com(:\d+)?$ $http_origin;
default "";
}
# 以下为仅为location部分配置
location / {
if ($request_method ~* '(GET|POST|DELETE|PUT)') {
add_header 'Access-Control-Allow-Credentials' "true" always;
add_header Access-Control-Allow-Origin $allow_origin_xxx always;
}
if ($request_method = OPTIONS ) {
add_header Access-Control-Allow-Origin $allow_origin_xxx always;
add_header 'Access-Control-Allow-Credentials' "true" always;
add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
return 200;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_read_timeout 150;
proxy_pass http://server;
}