如下

server {
listen 80;
server_name www.your.com;
root /data/web/www.your.com;
access_log /var/log/nginx/www.your.com_access.log;
error_log /var/log/nginx/www.your.com_error.log; set $cors_origin "";
if ( $http_origin ~ https?://.*\.(a|b)\.com ) {
set $cors_origin $http_origin;
}
add_header Access-Control-Allow-Origin $cors_origin;
}

  curl测试跨域是否生效,a.jpg要存在,查看Access-Control-Allow-Origin字段。

[root@HD1g-static-website /usr/local/nginx/conf/conf.d]# curl -I -H "Origin: https://test.a.com" https://www.your.com/a.jpg
HTTP/1.1 200 OK
Date: Sun, 05 May 2019 06:12:53 GMT
Content-Type: image/jpeg
Content-Length: 134100
Connection: keep-alive
Last-Modified: Tue, 05 Mar 2019 07:01:43 GMT
ETag: "5c7e1ed7-20bd4"
Expires: Tue, 04 Jun 2019 06:12:53 GMT
Cache-Control: max-age=2592000
Access-Control-Allow-Origin: https://test.a.com
Accept-Ranges: bytes

  一个较完整的CORS,引用自https://enable-cors.org/server_nginx.html

[root@HD1g-static-website /usr/local/nginx/conf/conf.d]# cat nginx_cors
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
} if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
} if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}

[nginx] CORS配置多域名的更多相关文章

  1. nginx下配置二级域名指向子目录

    今天终于把nginx的二级域名配置搞定了,哎之前在测试服务器上弄过一次,不过那个是在本地解析的hosts,把ip指向到域名上就ok,再在nginx.conf里改了下配置就好了,用同样的方法改了正式服务 ...

  2. Linux上Nginx部署配置--二级域名配置

    http://www.cnblogs.com/yaunion/archive/2013/03/16/2962385.html http://blog.csdn.net/LBinin/article/d ...

  3. Nginx配置多域名代理

    目的 当我们有多个站点需要对外网开放,每个站点的域名都不一样,然而我们只有一个外网ip.这种情况下,我们就可以使用一个Nginx来配置多域名代理.这种代理方式可以解决,在同一个端口上针对不同域名代理不 ...

  4. 阿里云服务器Linux CentOS安装配置(八)nginx安装、配置、域名绑定

    阿里云服务器Linux CentOS安装配置(八)nginx安装.配置.域名绑定 1.安装nginx yum -y install nginx 2.启动nginx service nginx star ...

  5. nginx配置多域名映射方法(本地hosts)

    本地测试网站的时候如果不想用localhost/xxxx的形式访问,可能就需要修改hosts文件来映射了,但是一个网站还好,假如有多个网站的话就不行了. 这时就需要配置多域名映射 比如hosts中配置 ...

  6. 利用nginx泛域名解析配置二级域名和多域名

    利用nginx泛域名解析配置二级域名和多域名 网站的目录结构为 html ├── bbs └── www html为nginx的安装目录下默认的存放源代码的路径. bbs为论坛程序源代码路径 www为 ...

  7. Nginx配置同一个域名同时支持http与https两种方式访问

    Nginx配置同一个域名http与https两种方式都可访问,证书是阿里云上免费申请的 server{listen 80;listen 443 ssl;ssl on;server_name 域名;in ...

  8. [亲测]ASP.NET Core 2.0怎么发布/部署到Ubuntu Linux服务器并配置Nginx反向代理实现域名访问

    前言 ASP.NET Core 2.0 怎么发布到Ubuntu服务器?又如何在服务器上配置使用ASP.NET Core网站绑定到指定的域名,让外网用户可以访问呢? 步骤 第1步:准备工作 一台Liun ...

  9. Nginx的虚拟服务器域名配置

    虚拟服务器名(server name)是通过指令server_name来指定的.在< Nginx是如何处理Request的?>一节中,我们讲到nginx分两步来匹配过来的Request请求 ...

随机推荐

  1. LOJ167 康托展开 题解

    题面 康托展开: 康托展开是一个全排列到一个自然数的双射,常用于构建哈希表时的空间压缩. 康托展开的实质是计算当前排列在所有由小到大全排列中的名次,因此是可逆的. X = A[0] * (n-1)! ...

  2. C#杀进程与之之子进程

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  3. Host xxx is not allowed to connect to this MariaDb server

    直接复制代码,无需修改 第一:// %:表示从任何主机连接到mysql服务器 GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'pass ...

  4. 常用Java中response.setContentType参数

    image/bmp BMP image/png PNG image/gif GIF image/jpeg JPEG image/tiff TIFF image/x-dcx DCX image/x-pc ...

  5. 使用CXF开发WebService程序的总结(四):基于bean的客户端和服务端代码的编写

    1. 在原服务端项目 ws_server中添加两个bean 1.1 添加两个类  User 和 Clazz   package com.lonely.pojo; public class User { ...

  6. webpack3 打包

    1. 基于 webpack 3.0 2.步骤.说明 2.1 webpack 本地初始化.安装基本包 npm init         >  package.json npm i  webpack ...

  7. iOS常用数学常量宏

    在实际工作中有些程序不可避免的需要使用数学函数进行计算,比如地图程序的地理坐标到地图坐标的变换.Objective-C做为ANSI C的扩展,使用C标准库头文件<math.h>中定义的数学 ...

  8. 2019-11-29-VisualStudio-使用新项目格式快速打出-Nuget-包

    title author date CreateTime categories VisualStudio 使用新项目格式快速打出 Nuget 包 lindexi 2019-11-29 10:15:25 ...

  9. Linux下网络设置

    1.临时IP配置 # ifconfig eth0   192.168.110.118    netmask 255.255.255.0   gateway 192.168.110.2    up # ...

  10. Spring Framework Part1

    初识Spring 1.Spring是一个支持IOC(Inversion of Control),DI(Dependency Injection),AOP(Aspect Oriented Program ...