美文网首页
记一次跨域引起的请求过程学习

记一次跨域引起的请求过程学习

作者: 在路上_seven | 来源:发表于2019-08-04 17:00 被阅读0次

最近在部署上线的过程中遇到了一个问题,项目在预发环境完美通过了测试,但是到线上环境就崩了。

浏览器报错:Access to fetch at http://24r03.com from origin htttp://www.baidu.com has been blocked CORS policy Access-Control-Allow-Origin header contains mutiple values 'http://www.baidu.com,* ' ,but only one is allowed ,Have the server send the header with a valid fetch the resource with CORS disable.

从报错来看,问题很明显,跨域处理中 ,allow-origin不可能指定多个域,更没有指定域和*共存的写法。

先排除代码侧问题  : 设置 allow-origin 时 只设置了一个域 ,并且使用的是 setHeader  并非addHeader ,

(setHeader 和 addHeader 的区别:是setHeader会覆盖之前设置的header 值  而 addHeader会直接添加)

代码不存在问题,那么根据预发没报错而线上出问题了,那就是可能跟环境有关 ,排查看浏览器返回的响应头里面的结果:

线上响应头

可以看到access-origin确实是多了 *  ,而却 相应的server 指向的是nginx 。

响应头对应的意思:https://blog.csdn.net/Boboma_dut/article/details/79741162

那么就有可能是NGINX 处理了一次跨域。并且是在响应通过后端之后。

查看线上环境的nginx的配置,发现nginx配置确实处理了跨域。

nginx处理跨域:原文:https://blog.csdn.net/envon123/article/details/83270277

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;

}

}

相关文章

  • 记一次跨域引起的请求过程学习

    最近在部署上线的过程中遇到了一个问题,项目在预发环境完美通过了测试,但是到线上环境就崩了。 浏览器报错:Acces...

  • axios发送俩次请求的原因

    其实跨域分为简单跨域请求和复杂跨域请求 简单跨域请求是不会发送options请求的 复杂跨域请求会发送一个预检请求...

  • AJAX出现两次请求 options和get|post

    跨域请求 允许跨域请求 preflighted request预请求(options) 跨域请求 XMLHttpR...

  • 用express实现CORS跨域

    跨域请求头 cors express 跨域请求

  • Ajax跨域请求与SpringMVC结合的一些坑

    Ajax跨域的概念就不在这边陈述了... Ajax跨域请求真正的请求之前会进行一次预请求OPTIONS请求,为了让...

  • 跨域

    1、跨域是什么 域指的是域名,向一个域发送请求,如果请求的域和当前域是不同域,就叫跨域;不同域之间的请求就叫跨域请...

  • Http访问跨域解决

    一、跨域科普 跨域,即跨站HTTP请求(Cross-site HTTP request),指发起请求的资源所在域不...

  • 跨域

    ??JSONP只能解决GET请求跨域,不能解决POST请求跨域问题,XHR2可以解决GET,POST方式的请求跨域...

  • axios 跨域请求(前后端分离)

    与其说是 axios 跨域请求,我觉得不如说是 webpack server 跨域请求,因为这里的跨域请求实现,还...

  • 跨域

    什么是跨域? 为什么会发生ajax跨域?浏览器限制跨域【发出的请求不是本域】XHR请求【json】 解决思路:1:...

网友评论

      本文标题:记一次跨域引起的请求过程学习

      本文链接:https://www.haomeiwen.com/subject/ktjydctx.html