跨站资源共享 CORS 跨域配置

创建
阅读 559

跨域资源共享(CORS) 是一种机制,它使用额外的 HTTP 头来告诉浏览器,让运行在一个 origin (domain) 上的 Web 应用被准许访问来自不同源服务器上的指定的资源。当一个资源从与该资源本身所在的服务器不同的域、协议或端口请求一个资源时,资源会发起一个跨域 HTTP 请求。具体参考 MDN 文档

下面记录一下如何配置 CORS

Nginx

可以在 httpserverlocation 中添加 add_header

  1. 允许单个域名跨域访问

    add_header    "Access-Control-Allow-Origin" "http://example.com";
    add_header    "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
    
  2. 允许多个域名跨域访问

    if ($http_origin ~ "^https?://(a.example.com|b.example.com)") {
       add_header    "Access-Control-Allow-Origin" $http_origin;
       add_header    "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
    }
    
  3. 允许所有域名跨域访问

    add_header    "Access-Control-Allow-Origin" "*";
    add_header    "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
    

PHP

server.php 文件头部添加如下代码:

  1. 允许单个域名跨域访问

    header('Access-Control-Allow-Origin:http://example.com');
    
  2. 允许多个域名跨域访问

    $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
    
    $allow_origin = array(
       'http://a.example.com',
       'https://a.example.com',
       'http://b.example.com',
       'https://b.example.com'
    );
    
    if(in_array($origin, $allow_origin)){
       header('Access-Control-Allow-Origin:'.$origin);
    }
    
  3. 允许所有域名访问

    header('Access-Control-Allow-Origin:*');
    

更新记录

  1. 2019-04-20 17:28:43 首次发布

参考链接

  1. Module ngx_http_headers_module
  2. PHP 通过 Access-Control-Allow-Origin 跨域

本文链接 https://www.yidiankuaile.com/post/cors-settings

最后更新