最近连续两个朋友问我跨域相关问题,我猜想可能不少朋友也遇到类似问题,我打算写个博客聊一下我实际使用的配置,

先说明一下,我并不太了解这配置,没精力去了解太多,但我觉得其中有一些关键的小注意点,可能有些初学者不太注意到,导致配置有问题,本文章可能只对新手有点帮助,如果你有好配置,欢迎评论回复,让大家学习!

Nginx的CORS配置,网上太多这配置了,但大家更多的复制粘贴、转发,几乎都是类似下面这三两行:

add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

这样有用么?有用,我以前这样使用也正常过,但后来还是遇到问题了,发现有些项目请求就不成功,也遇到有些浏览器成功,有些浏览器不成功;

我也在网上查找各种资料和调整写法,最后我调整好的写法,基本的使用没问题,我在项目中也一直使用着!

下面发一段我实际项目中的部分配置:

location /aoda-web {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-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';if ($request_method = 'OPTIONS') {add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}root   html;index  index.html index.htm;proxy_pass http://127.0.0.1:8080;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_set_header X-Forwarded-Proto $scheme;proxy_connect_timeout 5;
}

跨域相关的配置,主要是下面这部分:

 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-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';if ($request_method = 'OPTIONS') {add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}

下面简单讲解一下,以便大家配置成功!

1、Access-Control-Allow-Origin,这里使用变量 $http_origin取得当前来源域,大家说用“*”代表允许所有,我实际使用并不成功,原因未知;

2、Access-Control-Allow-Credentials,为 true 的时候指请求时可带上Cookie,自己按情况配置吧;

3、Access-Control-Allow-Methods,OPTIONS一定要有的,另外一般也就GET和POST,如果你有其它的也可加进去;

4、Access-Control-Allow-Headers,这个要注意,里面一定要包含自定义的http头字段(就是说前端请求接口时,如果在http头里加了自定义的字段,这里配置一定要写上相应的字段),从上面可看到我写的比较长,我在网上搜索一些常用的写进去了,里面有“web-token”和“app-token”,这个是我项目里前端请求时设置的,所以我在这里要写上;

5、Access-Control-Expose-Headers,可不设置,看网上大致意思是默认只能获返回头的6个基本字段,要获取其它额外的,先在这设置才能获取它;

6、语句“ if ($request_method = 'OPTIONS') { ”,因为浏览器判断是否允许跨域时会先往后端发一个 options 请求,然后根据返回的结果判断是否允许跨域请求,所以这里单独判断这个请求,然后直接返回;

好了,按我上面配置基本都可使用(有些朋友确定只百度复制了两行,然后说配置好了,跟前端朋友互扯),

下面发一个实际配置供参考,我做了少量更改,如下:

server {listen       80;server_name  xxx.com;location /xxx-web/papi {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-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';if ($request_method = 'OPTIONS') {add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}root   html;index  index.html index.htm;proxy_pass http://127.0.0.1:7071;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_set_header X-Forwarded-Proto $scheme;proxy_connect_timeout 5;}location /xxx-web {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-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';if ($request_method = 'OPTIONS') {add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}root   html;index  index.html index.htm;proxy_pass http://127.0.0.1:8080;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_set_header X-Forwarded-Proto $scheme;proxy_connect_timeout 5;}location / {root   /var/www/xxx/wechat/webroot;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}
}

(完)

我也说说Nginx解决前端跨域问题,正确的Nginx跨域配置(后端Nginx CORS跨域配置、CORS设置,后端允许跨域请求)相关推荐

  1. Nginx解决前端调用后端接口跨域问题

    Nginx解决前端调用后端接口跨域问题 参考文章: (1)Nginx解决前端调用后端接口跨域问题 (2)https://www.cnblogs.com/wangymd/p/11200746.html ...

  2. 如何用Nginx解决前端跨域问题?

    如何用Nginx解决前端跨域问题? 参考文章: (1)如何用Nginx解决前端跨域问题? (2)https://www.cnblogs.com/lovesong/p/10269793.html 备忘一 ...

  3. 将Vue项目部署在Nginx,解决前端路由、反向代理和静态资源问题

    将Vue项目部署在Nginx,解决前端路由.反向代理和静态资源问题 需求: 一台服务器,Linux 安装了Nginx 使用Vue脚手架编写的Vue项目 第一步:将Vue项目打包,然后将生成的dist文 ...

  4. Nginx解决前端跨域问题

    最近连续两个朋友问我跨域相关问题,我猜想可能不少朋友也遇到类似问题,我打算写个博客聊一下我实际使用的配置, 先说明一下,我并不太了解这配置,没精力去了解太多,但我觉得其中有一些关键的小注意点,可能有些 ...

  5. Nginx 解决前端跨域问题

    需求 Nginx跨域配置(后端Nginx CORS跨域配置.CORS设置,后端允许跨域请求) TL;DR 在nginx中的nginx.conf文件中配置下面内容即可 server {listen 30 ...

  6. 用nginx解决前端跨域问题

    写服务器写接口模拟后端 首先自己用node写了一个服务器接口,前提安装了node.js npm init -y npm i express 创建app.js ​ const express = req ...

  7. 使用 Nginx 构建前端日志统计服务(打点采集)服务

    使用 Nginx 构建前端日志统计服务(打点采集)服务 工作中经常会遇到需要"数据支撑"决策的时候,那么可曾想过这些数据从何而来呢?如果业务涉及 Web 服务,那么这些数据的来源之 ...

  8. 前端如何使用proxyTable和nginx解决跨域问题

    前端如何使用proxyTable和nginx解决跨域问题 参考文章: (1)前端如何使用proxyTable和nginx解决跨域问题 (2)https://www.cnblogs.com/webhmy ...

  9. nginx解决浏览器跨域问题_前端通过Nginx反向代理解决跨域问题

    在前面写的一篇文章SpringMVC解决跨域问题,我们探讨了什么是跨域问题以及SpringMVC怎么解决跨域问题,解决方式主要有如下三种方式: JSONP CORS WebSocket 可是这几种方式 ...

最新文章

  1. 论Docker swarm与Kubernetes孰强孰弱
  2. springmvc接收前台(可以是ajax)传来的数组list,map,set等集合,复杂对象集合等图文详解...
  3. 从零开始学习docker(十四)Docker Compose--build
  4. 基于Google Reader发展起来的个性化推荐系统之三大问题
  5. node --- koa、Mongoose、vue联系知识梳理
  6. SQL Server的聚集索引和非聚集索引
  7. Python中的线程及用法
  8. 传统企业如何面对电子商务的发展
  9. Hadoop三大发行版本简单介绍
  10. Python基础之列表、元组、字典、集合
  11. tdr 定位公式_基于土壤热导率定位监测容重的Thermo-TDR技术
  12. 大气科学领域必备的模型软件汇总丨WRF、WRF-CMAQ、WRF-Chem、WRF-Hydro、WRF DA、PMF、MCM、CAMx、SMOKE、CMIP6等
  13. linux重启 envi服务,envi 4.3在ubuntu edgy下启动错误的解决
  14. IT管理体系——战略、管理和服务
  15. 如何配置crontab每天早上6点和7点执行脚本
  16. Swan Song Gamma阶段博客目录
  17. 【计算机网络】期末复习试题
  18. [生而为人-思考] Knowledge Cooking 分享会记录 -1
  19. centOS 7下python2升级为python3
  20. Java入门第三季-综合实战:简易扑克牌游戏

热门文章

  1. 基于Java+Derby实现(PC)驾考试题管理系统【100010349】
  2. py文件和ipynb文件互相转换
  3. 3Dgame_homework7
  4. 音频放大器lm386
  5. DICOMRT-DiTools:clouddicom源码解析(1)
  6. CVPR2017 paper: A Hierarchical Approach for Generating Descriptive Image Paragraphs复现
  7. 看懂《C程序设计(第五版)学习辅导》第16章中介绍的用Visual Studio 2010对C程序进行编辑、编译、连接和运行的方法,并进行以下操作
  8. 数据分析思维分析方法和业务知识——分析方法(一)
  9. room+livedata+ViewModel+RecyclerView
  10. Lua(十六)——文件