博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx解决CORS跨域解决方案
阅读量:7164 次
发布时间:2019-06-29

本文共 3292 字,大约阅读时间需要 10 分钟。

首先通读下 MDN 关于 CORS 的 ,了解跨域的含义及简单请求和复杂请求等的定义。文中的内容不赘述,现在说解决方案。

通过定义我们可以,简单请求与复杂请求的差别是复杂请求会自动发出一个 OPTIONS 的预检请求,当请求得到确认后,才开始真正发送请求。

综上,我们要解决两个问题:

  1. OPTIONS 请求的正确响应
  2. 跨域请求正确响应

1、OPTIONS 请求的正确响应

解决的方式有多种,既可以在Web Server解决,也可以在源码层解决。因为问题比较普遍,故我们选择在Web Server解决,下面我们以 Nginx 为例,说明解决方案。

假设访问的地址为 /example , Nginx 配置如下:

location /example {    proxy_redirect off;    proxy_set_header Host $host;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    proxy_pass http://127.0.0.1:8080/;  }

为了解决跨域问题,添加如下内容:

location /example {+   if ($request_method = 'OPTIONS') {+       add_header Access-Control-Allow-Origin *;+       add_header Access-Control-Max-Age 1728000;+       add_header Access-Control-Allow-Methods GET,POST,OPTIONS;+       add_header Access-Control-Allow-Headers  'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';+       add_header Content-Type' 'text/plain; charset=utf-8';+       add_header Content-Length 0 ;+       return 204;+    }    proxy_redirect off;    proxy_set_header Host $host;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    proxy_pass http://127.0.0.1:8080/;   }

解释:

if ($request_method = 'OPTIONS') {...} 当请求方法为 OPTIONS 时:

1、添加允许源 Access-Control-Allow-Origin 为 * (可根据业务需要更改)
2、添加缓存时长 Access-Control-Max-Age,当下次请求时,无需再发送 OPTIONS 请求
3、添加允许的方法,允许的首部
4、添加一个内容长度为0,类型为 text/plain; charset=utf-8 , 返回状态码为 204 的首部
至此,完成 OPTIONS 请求的正确响应。

2、跨域请求正确响应

添加如下内容:

location /example {   if ($request_method = 'OPTIONS') {       add_header Access-Control-Allow-Origin *;       add_header Access-Control-Max-Age 1728000;       add_header Access-Control-Allow-Methods GET,POST,OPTIONS;       add_header Access-Control-Allow-Headers  'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';       add_header Content-Type' 'text/plain; charset=utf-8';       add_header Content-Length 0 ;       return 204;    }+   if ($http_origin ~* (https?://(.+\.)?(example\.com$))) {+     add_header  Access-Control-Allow-Origin $http_origin;+     add_header  Access-Control-Allow-Credentials true;+     add_header  Access-Control-Allow-Methods GET,POST,OPTIONS;+     add_header  Access-Control-Expose-Headers Content-Length,Content-Range;+   }    proxy_redirect off;    proxy_set_header Host $host;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    proxy_pass http://127.0.0.1:8080/;   }

解释:

if ($http_origin ~* (https?://(.+\.)?(example\.com$))) {...}, 当 origin 为合法域名(可根据业务调整或去除合法域名验证)时:

1、添加允许源Access-Control-Allow-Origin$http_origin (可根据业务需要更改)
2、添加允许认证Access-Control-Allow-Credentialstrue ,允许接收客户端 Cookie(可根据业务需要更改。 但要注意,当设置为true时,Access-Control-Allow-Origin 不允许设置为 *)
3、添加允许的方法,暴露的首部

至此,完成跨域请求正确响应。

以上,是对跨域请求在Web Server的解决方案,主要是通过响应 OPTIONS 方法和添加允许源来解决。

当然,如果本地开发中,可以在利用 webpack-dev-server 的 选项来快速解决跨域问题:

示例如下:

// webpack.congf.jsmodule.exports = {  //...  devServer: {    proxy: {      '/api': {        target: 'http://localhost:3000',        pathRewrite: {'^/api' : ''}      }    }  }}

当访问地址如 /api/foo?q=bar 时,则通过代理访问的实际地址是: http://localhost:3000/foo?q=bar

CORS跨域请求并不魔幻,理解 CORS 的含义,根据规则去找方法就迎刃而解了。希望能帮助到大家。

转自

转载于:https://blog.51cto.com/moerjinrong/2383651

你可能感兴趣的文章
类的继承和多态性作业
查看>>
locate,find,which,whereis,type命令区别
查看>>
hdu 4069 垃圾数独
查看>>
过滤垃圾评论
查看>>
java输入输出流总结 转载
查看>>
页面加载切换特效
查看>>
企业级 SpringBoot 教程 (十七)上传文件
查看>>
关于控制下拉框select只读的js控制
查看>>
关于Oracle、SqlServer 的sql递归查询
查看>>
字典的创建、删除以及获取字典中想要的元素
查看>>
Ajax + PHP 的简单应用
查看>>
面向对象
查看>>
通过日志文件查看百度蜘蛛是否抓取
查看>>
Linux下的SVN服务器搭建
查看>>
metasploit(MSF)对windows的ms17-010漏洞利用
查看>>
newFixedThreadPool
查看>>
pyhon的集合、字典、文件读写应用
查看>>
Hadoop集群搭建三(Linux虚拟机的网络配置和系统配置)
查看>>
iOS 图片截取 截屏
查看>>
CRM项目完成实现
查看>>