Access-Control-Allow-Origin Multiple Origin Domains?

前端 未结 30 2053
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 07:08

Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin header?

I\'m aware of the *, but it is too open. I rea

30条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 07:17

    For Nginx users to allow CORS for multiple domains. I like the @marshall's example although his anwers only matches one domain. To match a list of domain and subdomain this regex make it ease to work with fonts:

    location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ {
       if ( $http_origin ~* (https?://(.+\.)?(domain1|domain2|domain3)\.(?:me|co|com)$) ) {
          add_header "Access-Control-Allow-Origin" "$http_origin";
       }
    }
    

    This will only echo "Access-Control-Allow-Origin" headers that matches with the given list of domains.

提交回复
热议问题