使用map实现Nginx允许多个域名跨域

639 阅读1分钟

http

map $http_origin $allow_origin {
  default "";
  "~^(https?://localhost(:[0-9]+)?)" $1;
  "~^(https?://127.0.0.1(:[0-9]+)?)" $1;
  "~^(https?://172.10(.[\d]+){2}(:[0-9]+)?)" $1;
  "~^(https?://192.168(.[\d]+){2}(:[0-9]+)?)" $1;
  "~^(https?://.*.abc.com(:[0-9]+)?)" $1;
  "~^(https?://.*.geinino.com(:[0-9]+)?)" $1;
}

在server层添加

add_header Access-Control-Allow-Origin $allow_origin;