文章

nginx设置多个允许跨域的请求源

# 定义允许的源列表
map $http_origin $cors_origin {
    "https://youdomian.com" "https://youdomian.com";
    "http://localhost:3010" "http://localhost:3010";
}

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        # 检查是否匹配允许的 Origin,如果匹配则设置 CORS 头
        if ($cors_origin) {
            add_header 'Access-Control-Allow-Origin' $cors_origin;
            add_header 'Access-Control-Allow-Credentials' 'true';        
        }

        # 其他配置...
    }
}

许可协议:  CC BY 4.0