Nginx如何配置跨域(多个域名)

由 v233 发布于:2020/03/27

设需要允许来源为 localhost*.example.com 下所有二级域名的访问,在 nginx 中只需要类似这样配置即可:

location / {
        set $match "";
        # 支持http及https
        if ($http_origin ~* 'https?://(localhost|.*\.example\.com)') {
        set $match "true";
        }
        
        if ($match = "true") {
            add_header Access-Control-Allow-Origin "$http_origin";
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS,DELETE;
            add_header Access-Control-Allow-Credentials true;
        }
        # 处理OPTIONS请求
        if ($request_method = 'OPTIONS') {
            return 204;
        }
    }

原文:https://blog.csdn.net/moxiaomomo/article/details/82970004

点击查看&撰写评论