浏览器跨域详解

背景

当前流行前后端分离,很多时候前端域名与后端域名不一致,导致请求跨域被浏览器拦截。
浏览器控制台出现以下提示
在这里插入图片描述

原理及解决方案

在这里插入图片描述
页面发起跨域请求后,浏览器会先发起预检请求,预检通过后,在发起正式请求。如果预检请求不过,浏览器就会停止后面的业务请求,导致访问失败。

解释

  • 预检请求-如字面意思,就是在发起真实的业务请求前,先发送一个检测请求,如果服务端允许跨域,会在响应头中添加相应的header,表明自己允许跨域。
  • 预检请求不是必须的,请求符合特定特征时,才需要发送预检。具体的可以参考阮一峰老师的博客http://www.ruanyifeng.com/blog/2016/04/cors.html。
  • 即使通过了预检请求,服务端对于业务请求的响应也需要添加跨域头,不然还是会被浏览器拦截。

解决方案

跨域拦截是浏览器的行为与服务端无关,浏览器仅仅只是检查服务端的响应中是否包含跨域头,至于这个头是java服务生成的,还是nginx生成的都可以。
在这里插入图片描述
我们的组网如上,nginx做负债均衡,因为我们微服务较多,所以在nginx这一层做了配置来支持跨域,避免每个服务都去配置一遍。

if ($request_method = OPTIONS) {
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, HEAD, DELETE,TRACE,PUT,CONNECT';
    add_header 'Access-Control-Allow-Headers' xx
    return 200;
}

add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, HEAD, DELETE,TRACE,PUT,CONNECT';
add_header 'Access-Control-Allow-Headers' xx

闭坑指导

上述支持跨域的配置中未给每个配置项配置always,导致服务端返回非200时,跨域头未加上去,浏览器报跨域。
这个坑可以参考nginx的官方文档http://nginx.org/en/docs/http/ngx_http_headers_module.html

Syntax: add_header name value [always];
Default: —
Context: http, server, location, if in location
Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). Parameter value can contain variables.
There could be several add_header directives. These directives are inherited from the previous configuration level if and only if there are no add_header directives defined on the current level.
If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.

英文解释
在不添加always的情况下,add_header只对响应码为200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), 308 (1.13.0)这些生效(括号内是nginx版本)。也就是说当服务端返回响应异常,响应码不是上述之一的话,即使nginx有配跨域头信息,浏览器仍然会显示跨域错误。原因就是因为nginx对异常响应码添加add_header无效。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要配置Nginx实现浏览器跨域访问,可以按照以下步骤进行配置: 1. 在Nginx服务器上创建一个目录用于存放跨域访问资源。可以使用命令`mkdir /usr/share/nginx/corsmulti02/`来创建目录。 2. 在创建的目录中创建一个index.html文件,并添加内容。可以使用命令`echo '<h1>Corsmulti02</h1>' > /usr/share/nginx/corsmulti02/index.html`来创建文件并添加内容。 3. 在Nginx的配置文件中添加跨域访问的配置。可以使用add_header语法来配置响应头,使多个域名能够跨域访问资源。例如,可以在配置文件的相应位置使用以下语法进行配置: ``` add_header Access-Control-Allow-Origin "http://localhostsource01.odocker.com"; add_header Access-Control-Allow-Origin "http://source02.odocker.com"; ``` 其中,将"http://localhostsource01.odocker.com"和"http://source02.odocker.com"替换为允许跨域访问的域名。 通过以上配置,就可以实现浏览器跨域访问Nginx服务器上的资源了。在浏览器中访问"http://corsmulti02.linuxds.com/"即可跨域访问Nginx服务器上的资源。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Nginx跨域配置](https://blog.csdn.net/m0_67391683/article/details/126113742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Nginx跨域访问场景配置和防盗链详解](https://download.csdn.net/download/weixin_38737335/14091819)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值